[英]using the map function with timeit in Python
I am learning python and am looking at the difference in performance between using comprehension and the map function. 我正在学习python,正在研究使用理解和map函数之间的性能差异。
So far, I have this code: 到目前为止,我有以下代码:
import timeit
print timeit.timeit('[x for x in range(100)]',number = 10000)
def mapFunc(i):
print i
print timeit.timeit('map(mapFunc,range(100))',number = 10000)
After trying this, I have managed to get the list comprehension method to work correctly using timeit, however, I would like to compare this to the map function. 尝试此操作后,我设法使用timeit使列表理解方法正确运行,但是,我想将其与map函数进行比较。 When doing this, I get the error that mapFunc is not defined, and I do not know how to fix this issue.
这样做时,我得到未定义mapFunc的错误,并且我不知道如何解决此问题。 Can someone please help?
有人可以帮忙吗?
You should setup the function definition via the setup
argument: 您应该通过
setup
参数设置函数定义:
>>> setup='def mapFunc(): print(i)'
>>> timeit.timeit('map(mapFunc,range(100))', number=10000, setup=setup)
See the timeit
doc reference 请参阅
timeit
文档参考
PS Probably not a good idea to use map
for printing. PS使用
map
打印可能不是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.