【Python】 for, list_comprehension, map_function, generator_expression效率比较
2014-07-16 11:25
323 查看
<pre name="code" class="python">import time, sys reps = 1000 size = 10000 # 测试主体 def tester(func, *args): startTime = time.time() for i in range(reps): func(*args) elapsed = time.time() - startTime return elapsed # for def forStatement(): res = [] for x in range(size): res.append(abs(x)) # list comprehension def listComprehension(): res = [abs(x) for x in range(size)] # map function def mapFunction(): res = map(abs, range(size)) def generatorExpression(): res = list(abs(x) for x in range(size)) # 测试输出 print sys.version tests = (forStatement, listComprehension, mapFunction, generatorExpression) for testfunc in tests: print testfunc.__name__.ljust(20), '=>', tester(testfunc)
相关文章推荐
- Python___List Comprehension and Generator Function
- 判断List、Map、Set是否为空及效率比较
- Python中map和列表推导效率比较实例分析
- Python map和列表推导效率比较
- python中in在list和dict中查找效率比较
- 判断List、Map、Set是否为空及效率比较
- 判断List、Map、Set是否为空及效率比较
- Java高级个人笔记(判断List、Map、Set是否为空及效率比较)
- python list comprehension twos for loop 嵌套for循环
- Python中map和列表推导效率比较实例分析
- python技巧——list comprehension vs map
- 判断List、Map、Set是否为空及效率比较
- List for iterator 迭代 效率 比较
- Python基础,基本类型(整型,浮点数等)数据结构(List,dic(Map),Set,Tuple),控制语句(if,for,while,continue or break):来自学习资料
- list comprehension & generator expression
- 数组、List、ArrayList的使用以及for、foreach效率比较
- 判断List、Map、Set是否为空及效率比较
- 【python】map list for 运行时长测试
- python map和list comprehension 效率比较
- 比较Python 2 和 Python 3 在对list进行循环的执行效率