0

count = 10, 100, 1000, 10000

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

numstring 包含四个函数。

输出应如下所示(共 16 行):

numbers_string1,count = 10,min = 0.0001,实际值 = [0.0001, 0.0002, 0.0001, ...]

numbers_string1,count = 100,min = 0.002,实际值 = [0.002, 0.002, 0.003, ...]

……

numbers_string4,count = 10000,min = 0.1 实际值 = [....]

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

我的代码:

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()

谁能帮帮我。提前致谢

4

1 回答 1

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

告诉我好不好?

于 2014-03-09T14:32:36.953 回答