美文网首页python基础
Python性能优化

Python性能优化

作者: SnailTyan | 来源:发表于2017-09-18 18:39 被阅读49次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

Python使用非常方便、灵活,因此很受欢迎。但正因为如此,导致实现同一功能时,Python代码有很多写法,但不同的写法有不同的性能。因此写Python代码要有良好的习惯,多写高性能的代码。作者原来平常写Python代码也很随意,直到某天处理大量数据时半天看不到结果,究其原因,是Python代码的性能问题导致的。

1. 列表解析与列表重建

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import time

fr = open('words.txt')
t1 = time.time()
word_list = fr.readlines()
t2 = time.time()
print 'read file time: ', t2 -t1
fr.close()

# for循环构建列表
keywords = []
t1 = time.time()
for word in word_list:
    word = word.strip()
    keywords.append(word)
t2 = time.time()
print 'for loop time: ', t2 - t1

# 列表解析
t3 = time.time()
keywords = [word.strip() for word in word_list]
t4 = time.time()
print 'list pars time: ', t4 - t3

fr = open('words.txt')
t5 = time.time()
keywords = [word.strip() for word in fr.readlines()]
t6 = time.time()
fr.close()
print 'read file and list parse time: ', t6 - t5

print 'list length: ', len(word_list)

运行结果:

read file time:  0.0318450927734
for loop time:  0.137716054916
list pars time:  0.0910630226135
read file and list parse time:  0.124923944473
list length:  441669

结论:本次测试中,列表解析时间是for循环时间的2/3

2. 字符串拼接

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import time

fr = open('words.txt')
keywords = [word.strip() for word in fr.readlines()]
fr.close()

# 加号拼接字符串
t1 = time.time()
str1 = ''
for word in keywords:
    str1 += word
t2 = time.time()
print 'string concat time: ', t2 - t1

# join拼接字符串
t1 = time.time()
str2 = ''.join(keywords)
t2 = time.time()
print 'string join time: ', t2 - t1

print 'list length: ', len(keywords)

运行结果:

string concat time:  0.0814869403839
string join time:  0.0123951435089
list length:  441669

结论:本次测试中,join函数拼接字符串比+=拼接字符串快6倍多

3. range与xrange

  • range

python中range会直接生成一个list对象。

  • xrange

用法与range完全相同,所不同的是生成的不是一个数组,而是一个生成器,它的类型为xrange。在生成非常大的数字序列时,xrange不会马上开辟很大的一块内存空间。如果不是需要返回列表,则尽可能使用xrange

测试代码:

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

import time

t1 = time.time()
for i in range(1000000):
    pass
t2 = time.time()
print 'range time: ', t2 -t1

t1 = time.time()
for i in xrange(1000000):
    pass
t2 = time.time()
print 'xrange time: ', t2 -t1

测试结果:

range time:  0.0680990219116
xrange time:  0.0329170227051

结论:本次测试中,xrangerange快一倍多。

相关文章

  • Python-02进阶-07代码优化技巧

    代码优化技巧 优化原则 核心技巧 其他技巧 Python 代码性能优化技巧 常用代码优化技巧 sort()优于so...

  • Python-02进阶-06代码优化工具

    代码优化工具 Python作为高级编程语言,对于其性能要求也越来越注重。 本文总结: 性能优化的主要方法: 多进程...

  • Python性能优化

    文章作者:Tyan博客:noahsnail.com | CSDN | 简书 Python使用非常方便、灵活,因此很...

  • Python 性能优化

    循环的优化 有多重循环的尽量将内层的计算提到上一层。使用 xrange 比 range 节省大量的系统内存。可以不...

  • Python 性能优化

    姓名:唐来宾 学号:17101223417 转载http://mp.weixin.qq.com/s/S6BN8P4...

  • Android性能优化 - 消除卡顿

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化 - 内存优化 性能分析工具 - Tra...

  • Android性能优化 - 内存优化

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化- 内存优化 性能分析工具 - Trac...

  • 前端性能优化(中)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(上)...

  • 前端性能优化(下)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(中)...

  • Awesome Extra

    性能优化 性能优化模式 常见性能优化策略的总结 Spark 性能优化指南——基础篇 Spark 性能优化指南——高...

网友评论

本文标题:Python性能优化

本文链接:https://www.haomeiwen.com/subject/wkgbsxtx.html