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