Advertisement
DigitalMag

the simplest perfomance benchmark (python)

Oct 7th, 2020 (edited)
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. import sys
  2.  
  3. ##from time import clock
  4.  
  5. if sys.version_info.major == 2:
  6.     from time import clock
  7. else:
  8.     from time import monotonic as clock
  9.  
  10.  
  11. def profiler(func):
  12.     def wrapper(*args, **kwargs):
  13.         t = clock()
  14.         r = func(*args, **kwargs)
  15.         print('{} iterations completed for {} milliseconds'.format(args[0], round(clock() - t, 4) * 1000))
  16.         return r
  17.  
  18.     return wrapper
  19.  
  20. @profiler
  21. def main(s):
  22.     c = 0
  23.     for i in range(s):
  24.         c+=1
  25.     return c
  26.  
  27. if __name__ == '__main__':
  28.     for i in range(5):
  29.         main(10**6)
  30.  
  31. # => around 70ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement