Python内置数据类型list各方法的性能测试过程解析


Posted in Python onJanuary 07, 2020

这篇文章主要介绍了Python内置数据类型list各方法的性能测试过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

测试环境

本文所涉及的代码均在MacOS系统与CentOS7下测试,使用的Python版本为3.6.8。

测试模块

测试用的模块是Python内置的timeit模块:

timeit模块可以用来测试一小段Python代码的执行速度。

Timer类

class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>)

Timer是测量小段代码执行速度的类。

stmt参数是要测试的代码语句(statment);

setup参数是运行代码时需要的设置;

timer参数是一个定时器函数,与平台有关。

Timer类的timeit方法

timeit.Timer.timeit(number=1000000)

Timer类中测试语句执行速度的对象方法。number参数是测试代码时的测试次数,默认为1000000次。方法返回执行代码的平均耗时,一个float类型的秒数。

列表内置方法的性能测试

我们知道,生成一个列表可以使用列表生成式或者append、insert、extend这些方法,现在我们来看一下这些方法的执行效率:

from timeit import Timer


def test_list():
  lst = list(range(1000))

def test_generation():
  lst = [i for i in range(1000)]


def test_append():
  lst = []
  for i in range(1000):
    lst.append(i)

def test_add():
  lst = []
  for i in range(1000):
    lst += [i]

# 在列表的头部insert
def test_insert_zero():
  lst = []
  for i in range(1000):
    lst.insert(0,i)

# 在列表的尾部insert
def test_insert_end():
  lst = []
  for i in range(1000):
    lst.insert(-1,i)

def test_extend():
  lst = []
  lst.extend(list(range(1000)))


t1 = Timer("test_list()","from __main__ import test_list")
print(f"test_list takes {t1.timeit(number=1000)} seconds")

t2 = Timer("test_generation()","from __main__ import test_generation")
print(f"test_generation takes {t2.timeit(number=1000)} seconds")

t3 = Timer("test_append()","from __main__ import test_append")
print(f"test_append takes {t3.timeit(number=1000)} seconds")

t4 = Timer("test_add()","from __main__ import test_add")
print(f"test_add takes {t4.timeit(number=1000)} seconds")

t5 = Timer("test_insert_zero()","from __main__ import test_insert_zero")
print(f"test_insert_zero takes {t5.timeit(number=1000)} seconds")

t6 = Timer("test_insert_end()","from __main__ import test_insert_end")
print(f"test_insert_end takes {t6.timeit(number=1000)} seconds")

t7 = Timer("test_extend()","from __main__ import test_extend")
print(f"test_extend takes {t7.timeit(number=1000)} seconds")

我们先看看在MacOS系统下,执行上面这段代码的结果:

"""
test_list takes 0.012904746999993222 seconds
test_generation takes 0.03530399600003875 seconds
test_append takes 0.0865129750000051 seconds
test_add takes 0.08066114099983679 seconds
test_insert_zero takes 0.30594958500023495 seconds
test_insert_end takes 0.1522782449992519 seconds
test_extend takes 0.017534753999825625 seconds
"""

我们可以看到:直接使用list方法强转的效率最高,其次是使用列表生成式,而append与直接加的方式紧随其后并且二者的效率相当;insert方法的执行效率最低——并且从头插入的效率要低于从尾部插入的效率!最后我们将强转的列表使用extend方法放入到新的列表中的过程效率并没有减少多少。

然后试试在Linux系统下的执行结果:

Python内置数据类型list各方法的性能测试过程解析

列表pop方法的性能测试

pop可以从第0各位置删除元素,也可以从最后位置删除元素(默认删除最后面的元素),现在我们来测试一下两种从不同位置删除元素的性能对比:

from timeit import Timer

def test_pop_zero():
  lst = list(range(2000))
  for i in range(2000):
    lst.pop(0)


def test_pop_end():
  lst = list(range(2000))
  for i in range(2000):
    lst.pop()
t1 = Timer("test_pop_zero()","from __main__ import test_pop_zero")
print(f"test_pop_zero takes {t1.timeit(number=1000)} seconds")

t2 = Timer("test_pop_end()","from __main__ import test_pop_end")
print(f"test_pop_end takes {t2.timeit(number=1000)} seconds")

在MacOS下程序的执行结果为:

test_pop_zero takes 0.5015365449999081 seconds

test_pop_end takes 0.22170215499954793 seconds

然后我们来试试Linux系统中的执行结果:

Python内置数据类型list各方法的性能测试过程解析

可以看到:从列表的尾部删除元素的效率要比从头部删除的效率高很多!

关于列表insert方法的一个小坑

如果想使用insert方法生成一个列表[0,1,2,3,4,5]的话(当然使用insert方法效率会低很多,建议使用其他的方法)会有一个这样的问题,在此记录一下:

def test_insert():
  lst = []
  for i in range(6):
    lst.insert(-1,i)
    print(lst)

test_insert()

结果竟然是这样的——第一个元素竟然一直在最后!

[0]
[1, 0]
[1, 2, 0]
[1, 2, 3, 0]
[1, 2, 3, 4, 0]
[1, 2, 3, 4, 5, 0]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python基础入门之seed()方法的使用
May 15 Python
Python中的with...as用法介绍
May 28 Python
Python 3中的yield from语法详解
Jan 18 Python
Python调用系统底层API播放wav文件的方法
Aug 11 Python
python实现简单的文字识别
Nov 27 Python
Python3实现的旋转矩阵图像算法示例
Apr 03 Python
Python之time模块的时间戳,时间字符串格式化与转换方法(13位时间戳)
Aug 12 Python
python+selenium 脚本实现每天自动登记的思路详解
Mar 11 Python
python对XML文件的操作实现代码
Mar 27 Python
python时间time模块处理大全
Oct 25 Python
Pycharm如何自动生成头文件注释
Nov 14 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
Jan 06 Python
python模拟实现斗地主发牌
Jan 07 #Python
python全局变量引用与修改过程解析
Jan 07 #Python
python__new__内置静态方法使用解析
Jan 07 #Python
Python常用模块sys,os,time,random功能与用法实例分析
Jan 07 #Python
python单例设计模式实现解析
Jan 07 #Python
Python字典生成式、集合生成式、生成器用法实例分析
Jan 07 #Python
linux环境下安装python虚拟环境及注意事项
Jan 07 #Python
You might like
ThinkPHP与PHPExcel冲突解决方法
2011/08/08 PHP
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
JavaScript初学者应注意的七个细节详细介绍
2012/12/27 Javascript
JavaScript中的await/async的作用和用法
2016/10/31 Javascript
javascript中Number的方法小结
2016/11/21 Javascript
vue.js打包之后可能会遇到的坑!
2018/06/03 Javascript
JS 正则表达式验证密码、邮箱格式的实例代码
2018/10/28 Javascript
基于Vue实现平滑过渡的拖拽排序功能
2019/06/12 Javascript
Vue 前端实现登陆拦截及axios 拦截器的使用
2019/07/17 Javascript
解决layui-table单元格设置为百分比在ie8下不能自适应的问题
2019/09/28 Javascript
element-ui和vue表单(对话框)验证提示语(残留)清除操作
2020/09/11 Javascript
原生js+canvas实现验证码
2020/11/29 Javascript
Python标准库os.path包、glob包使用实例
2014/11/25 Python
Python实现方便使用的级联进度信息实例
2015/05/05 Python
Python pickle模块用法实例分析
2015/05/27 Python
Django验证码的生成与使用示例
2017/05/20 Python
Python 结巴分词实现关键词抽取分析
2017/10/21 Python
Django使用Mysql数据库已经存在的数据表方法
2018/05/27 Python
python批量赋值操作实例
2018/10/22 Python
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
2019/06/19 Python
PyQt5 QTableView设置某一列不可编辑的方法
2019/06/25 Python
Python叠加两幅栅格图像的实现方法
2019/07/05 Python
Django中ajax发送post请求 报403错误CSRF验证失败解决方案
2019/08/13 Python
Python django框架输入汉字,数字,字符生成二维码实现详解
2019/09/24 Python
django中瀑布流写法实例代码
2019/10/14 Python
python利用Excel读取和存储测试数据完成接口自动化教程
2020/04/30 Python
Python3使用Selenium获取session和token方法详解
2021/02/16 Python
优质有机椰子产品:Dr. Goerg
2019/09/24 全球购物
策划助理岗位职责
2013/11/18 职场文书
幼儿园毕业典礼主持词
2014/03/21 职场文书
行政人事主管岗位职责
2015/04/11 职场文书
因身体原因离职的辞职信范文
2015/05/12 职场文书
会计岗位工作总结
2015/08/12 职场文书
python glom模块的使用简介
2021/04/13 Python
Python利器openpyxl之操作excel表格
2021/04/17 Python
Python多线程 Queue 模块常见用法
2021/07/04 Python