Advertisement
Guest User

benchmark.py

a guest
Aug 27th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. #! /usr/bin/python
  2.  
  3. import time
  4. import pyc
  5.  
  6. ITERATIONS = 100000000
  7.  
  8. iterative = xrange(ITERATIONS)
  9.  
  10. def test():
  11.     return None
  12.  
  13. def calc_period_nsec(start, stop):
  14.     return int(round(((stop - start) * 1e9) / ITERATIONS, 0))
  15.  
  16. # measure no-load loop period:
  17. start = time.time()
  18. for _ in iterative:
  19.     pass
  20. stop = time.time()
  21. period_noload = calc_period_nsec(start, stop)
  22. print 'no-load loop period:\t', period_noload, 'nsec'
  23.  
  24. # now with C-invocation:
  25. start = time.time()
  26. for _ in iterative:
  27.     pyc.test()
  28. stop = time.time()
  29. period_test_c = calc_period_nsec(start, stop)
  30. #print 'c loop period:\t', period_test_c, 'nsec'
  31.  
  32. # and now with Python-invocation:
  33. start = time.time()
  34. for _ in iterative:
  35.     test()
  36. stop = time.time()
  37. period_test_py = calc_period_nsec(start, stop)
  38. #print 'py loop period:\t', period_test_py, 'nsec'
  39.  
  40. # ok, compute pure time and print it:
  41. pure_time_c = period_test_c - period_noload
  42. pure_time_py = period_test_py - period_noload
  43. print 'function invocation time [nsec]:'
  44. print 'c\t', pure_time_c
  45. print 'py\t', pure_time_py
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement