难以使用Timeit模块

时间:2014-02-28 10:26:05

标签: python-2.7 timer timeit

count = 10, 100, 1000, 10000

对于每个count值,我需要使用timeit执行该方法5次并打印出最小值和实际的5个值。

numstring包含四个函数。

输出应该看起来像(总共16行):

numbers_string1,count = 10,min = 0.0001,actuals = [0.0001,0.0002,0.0001,...]

numbers_string1,count = 100,min = 0.002,actuals = [0.002,0.002,0.003,...]

...

numbers_string4,count = 10000,min = 0.1 actuals = [....]

为此,我尝试了这种方式:

我的代码:

from numstring import *
import timeit

def profile_timeit():
    funcs_list = [numbers_string1, numbers_string2, numbers_string3, num_strings4]
    for i in funcs_list:
        for count in [10, 100, 1000, 10000]:
            actuals = timeit.timeit(stmt='i(count)', number=4, setup='from __main__ import *')
            print "{0} count = {1} \t min = {2} \t actuals = {3}".format(i, count, min(actuals), actuals)
        print "\n"
if __name__ == "__main__":
    profile_timeit()

任何人都可以帮帮我。提前致谢

1 个答案:

答案 0 :(得分:1)

你可以试试这个....

from numstring import *
import timeit
def profile_timeit():
    funcs = [numbers_string1,numbers_string2,numbers_string3,num_strings4]
    count = [10,100,1000,10000]
    for func in funcs:
        for cnt in count:
            for i in xrange(5):
                t=timeit.Timer(stmt = "%s(%d)"%(func.__name__, cnt), setup = "from __main__ import %s"%(func.__name__, ))
                tms = t.repeat(repeat=5, number=1)
            print "%s, count = %d, min = %s , actuals = %s" % (func.__name__, cnt, min(tms), tms)
    pass

告诉我它是否合适?