timeit模块用于测试一段代码的执行效率

1.Timer类

Timer 类:

__init__(stmt="pass", setup="pass", timer=default_timer)

stmt 是执行语句,setup 是导入执行语句环境

print_exc(file=None)
timeit(number=default_number)

返回测试所用秒数,number 是每个测试中调用被计时语句的次数

repeat(repeat=default_repeat, number=default_number)

返回测试所用秒数列表,repeat 是重复整个测试的次数,number 是每个测试中执行语句的次数

快捷执行的方法:

timeit.timeit('time.time()','import time',number=10)
#这里会执行两次测试,每次测试都执行time.time()语句10次
timeit.repeat('time.time()','import time',repeat=2,number=10)

DEMO

import timeit
def f1():
for i in range(1000):
pass
def f2():
for i in xrange(1000):
pass
t=timeit.Timer('f1()','from __main__ import f1')
t1=timeit.Timer('f2()','from __main__ import f2')
print t.timeit()
print t1.timeit()

查看一个脚本的执行效率

python -m cProfile test_cprofile.py

2.测试Python不同用法的性能

代码:

#copy与deepcopy测试
print 'test copy and deepcopy'
import copy
x=range(1000)
print 'copy',timeit.timeit('copy.copy(x)','from __main__ import copy,x',number=100)
print 'deepcopy',timeit.timeit('copy.deepcopy(x)','from __main__ import copy,x',number=100)
#json和eval测试
import json
import ast
print 'test json and eval'
d=dict([(i,i) for i in xrange(1000)])
def test_json():
json.loads(json.dumps(d))
def test_ast():
ast.literal_eval(str(d))
print 'json',timeit.timeit('test_json()','from __main__ import test_json',number=100)
print 'ast',timeit.timeit('test_ast()','from __main__ import test_ast',number=100)
#while 1 和while True测试
print 'test while 1 and while True'
def while_one():
i=1000
while 1:
i-=1
if i<1:
break
def while_true():
i=1000
while True:
i-=1
if i<1:
break
print 'while 1',timeit.timeit('while_one()','from __main__ import while_one',number=1000)
print 'while true',timeit.timeit('while_true()','from __main__ import while_true',number=1000)
#测试map和列表解析
print 'map',timeit.timeit('map(lambda x:x+10,xrange(10000))',number=1000)
print 'list parse',timeit.timeit('[x+10 for x in xrange(10000)]',number=1000)
#测试filter和列表解析
print 'filter',timeit.timeit('filter(lambda x:x<5000,xrange(10000))',number=1000)
print 'list parse',timeit.timeit('[x for x in xrange(10000) if x<5000]',number=1000)
#测试zip和列表解析
d=[(i,i+1,i+2) for i in xrange(10000)]
print 'zip',timeit.timeit('zip(*d)[0]','from __main__ import d',number=1000)
print 'list parse',timeit.timeit('[x[0] for x in d]','from __main__ import d',number=1000)

测试结果:

test copy and deepcopy
copy 0.000615931092709
deepcopy 0.215742113851
test json and eval
json 0.0845840788228
ast 0.603505149528
test while 1 and while True
while 1 0.0920688664622
while true 0.107391902198
map 1.89678050601
list parse 0.741696814336
filter 1.42262613537
list parse 0.631220298896
zip 1.26682006617
list parse 0.993506476463

结论

  1. copy比deepcopy快
  2. json比ast快
  3. while 1比while True快
  4. 列表解析比map,filter,zip快

    5

    r1=[string[i:i+width] for i in xrange(0,len(string),width)]

    r2=re.findall(r".{%s}"%width,string)

    r1比r2快

参考博客

Python执行效率测试模块timei的使用方法与与常用Python用法的效率比较的更多相关文章

  1. Python 执行 javascript PyExecJS 模块

    PyExecJS 安装 pip install PyExecJS PyExecJS 的基本使用: >>> import execjs >>> execjs.eval ...

  2. 转载 jenkins执行selenium 测试 浏览器不显示解决方法

    原文地址: http://blog.csdn.net/achang21/article/details/45096003 The web browser doesn't show while run ...

  3. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  4. python里面的xlrd模块

    ♦python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 今天就先来说一下xlrd模块: 一.安装xlrd模块 ♦ 到python官网下载 ...

  5. python中日志logging模块的性能及多进程详解

    python中日志logging模块的性能及多进程详解 使用Python来写后台任务时,时常需要使用输出日志来记录程序运行的状态,并在发生错误时将错误的详细信息保存下来,以别调试和分析.Python的 ...

  6. python执行shell获取硬件参数写入mysql

    最近要获取服务器各种参数,包括cpu.内存.磁盘.型号等信息.试用了Hyperic HQ.Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy. 于是乎想到用python ...

  7. python里面的xlrd模块详解(一)

    那我就一下面积个问题对xlrd模块进行学习一下: 1.什么是xlrd模块? 2.为什么使用xlrd模块? 3.怎样使用xlrd模块? 1.什么是xlrd模块? python操作excel主要用到xlr ...

  8. python标准日志模块logging的使用方法

    参考地址 最近写一个爬虫系统,需要用到python的日志记录模块,于是便学习了一下.python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果 ...

  9. Python 中 logging 日志模块在多进程环境下的使用

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Python 中 logging 日志模块在多进程环境下的使用 使用 Pytho ...

随机推荐

  1. coherence配置说明

    经过上篇 coherence初识 ,最近算是和coherence杠上了,针对coherence3.5.3这个版本,把学到的东西整理下 1. 这个jar包有点大,4M多,首先打开coherence.ja ...

  2. Linux 根文件系统的制作

    一.建立根文件系统目录与文件 1. 创建目录 #mkdir rootfs #cd rootfs #mkdir bin dev etc lib proc sbin sys usr mnt tmp var ...

  3. 减小iOS应用程序的大小

    减小iOS应用程序的大小 本文译自:Reducing the size of my App Q: 怎样才能让我的程序安装包小一点,让程序的下载和安装更快速? A: 本文收集了一些减小程序安装包大小的相 ...

  4. Linux 命令 - rm: 删除文件和目录

    命令格式 rm [OPTION]... FILE... 命令参数 -f, --force 强制删除,忽略不存在的文件,不会提示. -i, --interactive 没次删除文件时,提示用户确认. - ...

  5. 浅谈Oracle 性能优化

    基于大型Oracle数据库应用开发已有6个年头了,经历了从最初零数据演变到目前上亿级的数据存储.在这个经历中,遇到各种各样的性能问题及各种性能优化. 在这里主要给大家分享一下数据库性能优化的一些方法和 ...

  6. Unity Rigidbody 刚体中的Angular Drag和Freeze Position/Rotation

    Rigidbody中 Angular Drag  (角阻力):同样指的是空气阻力,只不过是用来阻碍物体旋转的.如果设置成无限的话,物体会立即停止旋转.如果设置成0,物体在上升过程中,会发生侧翻旋转. ...

  7. unity3d首次倒入工程文件出错Opening file Library/FailedAssetImports.txt failed解决方法

    打开unity3d,首次倒入工程到unity编辑器,但是频繁弹出“Opening file Library/FailedAssetImports.txt failed”的错误对话框,很麻烦. 解决方法 ...

  8. 北大ACM(POJ1004-Financial Management)

    Question:http://poj.org/problem?id=1004问题点:求平均值及格式化输出. Memory: 248K Time: 0MS Language: C++ Result: ...

  9. [转]WCF 4 安全性和 WIF 简介

      转自:http://www.cnblogs.com/WizardWu/archive/2010/10/04/1841793.html 本帖简介 .NET 新一代的 Windows Identity ...

  10. Linux文件系统结构

    准备写个Linux基础知识总结, 第一个想到的就是整理一个常用系统文件夹结构的说明,园子里“Aric小屋”的结构图整理的不错,我就不重复整理了,故借用一下