/Tools/pybench/clockres.py

http://unladen-swallow.googlecode.com/ · Python · 43 lines · 31 code · 4 blank · 8 comment · 8 complexity · d7fb8dea70527c452411e317d62389ca MD5 · raw file

  1. #!/usr/bin/env python
  2. """ clockres - calculates the resolution in seconds of a given timer.
  3. Copyright (c) 2006, Marc-Andre Lemburg (mal@egenix.com). See the
  4. documentation for further information on copyrights, or contact
  5. the author. All Rights Reserved.
  6. """
  7. import time
  8. TEST_TIME = 1.0
  9. def clockres(timer):
  10. d = {}
  11. wallclock = time.time
  12. start = wallclock()
  13. stop = wallclock() + TEST_TIME
  14. spin_loops = range(1000)
  15. while 1:
  16. now = wallclock()
  17. if now >= stop:
  18. break
  19. for i in spin_loops:
  20. d[timer()] = 1
  21. values = d.keys()
  22. values.sort()
  23. min_diff = TEST_TIME
  24. for i in range(len(values) - 1):
  25. diff = values[i+1] - values[i]
  26. if diff < min_diff:
  27. min_diff = diff
  28. return min_diff
  29. if __name__ == '__main__':
  30. print 'Clock resolution of various timer implementations:'
  31. print 'time.clock: %10.3fus' % (clockres(time.clock) * 1e6)
  32. print 'time.time: %10.3fus' % (clockres(time.time) * 1e6)
  33. try:
  34. import systimes
  35. print 'systimes.processtime: %10.3fus' % (clockres(systimes.processtime) * 1e6)
  36. except ImportError:
  37. pass