python - How to use timeit module -
i understand concept of timeit
not sure how implement in code.
how can compare 2 functions, insertion_sort
, tim_sort
, timeit
?
the way timeit works run setup code once , make repeated calls series of statements. so, if want test sorting, care required 1 pass @ in-place sort doesn't affect next pass sorted data (that, of course, make timsort shine because performs best when data partially ordered).
here example of how set test sorting:
>>> import timeit >>> setup = ''' import random random.seed('slartibartfast') s = [random.random() in range(1000)] timsort = list.sort ''' >>> print min(timeit.timer('a=s[:]; timsort(a)', setup=setup).repeat(7, 1000)) 0.334147930145
note series of statements makes fresh copy of unsorted data on every pass.
also, note timing technique of running measurement suite 7 times , keeping best time -- can reduce measurement distortions due other processes running on system.
those tips using timeit correctly. hope helps :-)
Comments
Post a Comment