python timeit模块
timeit模块
timeit模块可以用来测试一小段Python代码的执行速度。
class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>)
Timer是测量小段代码执行速度的类。
stmt参数是要测试的代码语句(statment);
setup参数是运行代码时需要的设置;
timer参数是一个定时器函数,与平台有关。
timeit.Timer.timeit(number=1000000)
Timer类中测试语句执行速度的对象方法。number参数是测试代码时的测试次数,默认为1000000次。方法返回执行代码的平均耗时,一个float类型的秒数。
def test1():
l = []
for i in range(1000):
l = l + [i]
def test2():
l = []
for i in range(1000):
l.append(i)
from timeit import Timer
#__main__代表启动文件
t1 = Timer("test1()", "from __main__ import test1")
print("concat ",t1.timeit(number=1000), "seconds")
t2 = Timer("test2()", "from __main__ import test2")
print("append ",t2.timeit(number=1000), "seconds")
runfile('L:/diango/mysite/untitled0.py', wdir='L:/diango/mysite')
concat 2.1704582298644333 seconds
append 0.10535473034465159 seconds
相关文章
- python中,numeric(数字类型)和integer(整型)的区别
- windows系统下Python环境的搭建
- Python变量/运算符/函数/模块/string
- Python是什么?为什么这么火?
- Python不香吗?你竟然要转行?
- Python基础知识点,建议收藏
- Python rrdtool 模块数据处理详解
- python cPickle 与 pickle模块序列化详解
- Python数据可视化2.4 用数据编写有趣的故事
- 少儿学编程系列---使用python turtle画玫瑰
- 设计原则Python
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python模块之lib2to3(py2转py3自动化工具)
- python自动化运维常用模块安装
- Python网络爬虫规则之Robots协议
- 《Python入门经典》——1.4 小结
- python 模块的使用
- 「基于Python技术的智慧中医商业项目」Django后端新闻应用设计
- python 安装impala包
- python的logging 模块的propagate设置
- PIL 和 python-opencv 从内存字节码中读取图片并转为np.array格式
- python中的time模块测试程序运行时间
- Python之道(一)之安装Python
- 【Python】快速获取域名、请求路径及参数
- [Python] 二维数组行列转换