Python中map和列表推导效率比较实例分析,python实例分析
Python中map和列表推导效率比较实例分析,python实例分析
本文实例讲述了Python中map和列表推导效率比较。分享给大家供大家参考。具体分析如下:
直接来测试代码吧:
#!/usr/bin/env python # -*- coding: utf-8 -*- # list comprehension and map import time def test(f, name): st = time.time() f() print '%s %ss'%(name, time.time()-st) TIMES = 1000 ARR = range(10000) def tmap(): i = 0 while (i<TIMES): map(lambda x:x, ARR) i = i+1 def tlst(): i = 0 while (i<TIMES): [x for x in ARR] i = i+1 test(tmap, "map") test(tlst, "lst")
在我电脑上的测试结果:
map 1.06299996376s lst 0.296000003815s
很明显列表推导比map操作会快很多,都三倍速度了
希望本文所述对大家的Python程序设计有所帮助。
相关内容
- Python求两个文本文件以行为单位的交集、并集与差集的
- Python操作串口的方法,python操作串口
- Python合并两个字典的常用方法与效率比较,python合并
- Python使用urllib2模块实现断点续传下载的方法,pythonur
- Python实现的使用telnet登陆聊天室实例,pythontelnet
- 在Python的struct模块中进行数据格式转换的方法,python
- Python基于Tkinter的HelloWorld入门实例,tkinterhelloworld
- Python基于Tkinter实现的记事本实例,pythontkinter实例
- Python实现的彩票机选器实例,python彩票机
- Python抽象类的新写法,Python抽象类写法
评论关闭