我试图在python2.7中对两个可比较的语句进行基准测试。你知道吗
一条语句打印从内存中检索的数字列表。另一条语句打印从同一服务器上维护的redis列表中检索到的相同号码的列表。你知道吗
例如
pylist1 = [1,2,3,4,5]
pylist2 = my_server.lrange("nums:5",0,-1) #where nums:5 = [1,2,3,4,5]
如何使用timeit对print pylist1
和print pylist2
进行基准测试?你知道吗
我正在python shell中尝试以下操作:
import timeit
import redis
POOL = redis.ConnectionPool(host='127.0.0.1',port=6379,db=0)
my_server = redis.Redis(connection_pool=POOL)
pylist1 = [1,2,3,4,5]
pylist2 = my_server.lrange("nums:5",0,-1)
print min(timeit.Timer('print pylist1').repeat(7,1000))
这只会与NameError: global name 'pylist1' does not exist
崩溃。这很奇怪,因为print pylist1
工作得很好!?在做我想做的事情时需要帮助。你知道吗
Timer
的文档说明:这告诉您,
Timer
执行的语句在默认情况下是在它自己的名称空间中执行的,该名称空间既不能访问外部名称空间,也不能修改它们。要更改此设置,可以使用setup语句导入所需的名称:或者只指定使用当前命名空间来执行代码:
请注意,如果您正在计时的语句修改了变量,它们实际上会在模块中更改。 最好使用
globals
的副本:在这种情况下,所分析的代码不会修改全局变量
相关问题 更多 >
编程相关推荐