blob: d7f1ac8c93747aa86982aeda28678b8e0a013f07 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001#!/usr/bin/env python
2
3""" clockres - calculates the resolution in seconds of a given timer.
4
5 Copyright (c) 2006, Marc-Andre Lemburg (mal@egenix.com). See the
6 documentation for further information on copyrights, or contact
7 the author. All Rights Reserved.
8
9"""
10import time
11
12TEST_TIME = 1.0
13
14def clockres(timer):
15 d = {}
16 wallclock = time.time
17 start = wallclock()
18 stop = wallclock() + TEST_TIME
19 spin_loops = range(1000)
20 while 1:
21 now = wallclock()
22 if now >= stop:
23 break
24 for i in spin_loops:
25 d[timer()] = 1
Georg Brandlbf82e372008-05-16 17:02:34 +000026 values = sorted(d.keys())
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027 min_diff = TEST_TIME
28 for i in range(len(values) - 1):
29 diff = values[i+1] - values[i]
30 if diff < min_diff:
31 min_diff = diff
32 return min_diff
33
34if __name__ == '__main__':
Guido van Rossum486364b2007-06-30 05:01:58 +000035 print('Clock resolution of various timer implementations:')
36 print('time.clock: %10.3fus' % (clockres(time.clock) * 1e6))
37 print('time.time: %10.3fus' % (clockres(time.time) * 1e6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038 try:
39 import systimes
Guido van Rossum486364b2007-06-30 05:01:58 +000040 print('systimes.processtime: %10.3fus' % (clockres(systimes.processtime) * 1e6))
Thomas Wouters0e3f5912006-08-11 14:57:12 +000041 except ImportError:
42 pass