【timeit】使用python中计时器Timer来计算

timeit — Measure execution time of small code snippets

class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)
To measure the execution time of the first statement, use the timeit() method. The repeat() and autorange() methods are convenience methods to call timeit() multiple times.
timeit(number=1000000)

timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()


>>> import timeit
>>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
>>> t.timeit()
0.3955516149999312
>>> t.repeat()
[0.40183617287970225, 0.37027556854118704, 0.38344867356679524, 0.3712595970846668, 0.37866875250654886]

猜你喜欢

转载自blog.csdn.net/weixin_42317507/article/details/89308085
今日推荐