Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' timeit_lambda_simple101.py
- using Python module timeit on a simple statement
- (by default timeit turns off garbage collection)
- if you have iPython running it is even simpler ...
- In [1]: %timeit not 9999991 & 1
- 10000000 loops, best of 3: 39.9 ns per loop
- In [2]: %timeit 9999991 % 2 == 0
- 10000000 loops, best of 3: 60 ns per loop
- '''
- import timeit
- '''
- # use n=1000000 passes (this is also the default)
- # this way the result will be time per pass in microseconds
- elapsed = lambda stmt, n=1000000: timeit.Timer(stmt).timeit(n)
- '''
- # or even simpler
- # using default 1000000 passes --> micro-seconds/pass
- elapsed = lambda stmt: timeit.Timer(stmt).timeit()
- stmt = "x = (12345679 * 63)//7"
- # run and show the timeit test
- print("'%s' took %0.3f micro-seconds" % (stmt, elapsed(stmt)))
- stmt = "not 9999991 & 1" # True if even
- # run and show the timeit test
- print("'%s' took %0.3f micro-seconds" % (stmt, elapsed(stmt)))
- stmt = "9999991 % 2 == 0" # True if even
- # run and show the timeit test
- print("'%s' took %0.3f micro-seconds" % (stmt, elapsed(stmt)))
- ''' result (Python 3.4.1 64bit) -->
- 'x = (12345679 * 63)//7' took 0.022 micro-seconds
- 'not 9999991 & 1' took 0.033 micro-seconds
- '9999991 % 2 == 0' took 0.054 micro-seconds
- '''
Advertisement
Add Comment
Please, Sign In to add comment