Show More
@@ -1,121 +1,121 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Utilities for timing code execution. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | 10 | # the file COPYING, distributed as part of this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | import time |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Code |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | # If possible (Unix), use the resource module instead of time.clock() |
|
24 | 24 | try: |
|
25 | 25 | import resource |
|
26 | 26 | except ImportError: |
|
27 | 27 | resource = None |
|
28 | 28 | |
|
29 | 29 | # Some implementations (like jyputerlite) don't have getrusage |
|
30 | 30 | if resource is not None and hasattr(resource, "getrusage"): |
|
31 | 31 | def clocku(): |
|
32 | 32 | """clocku() -> floating point number |
|
33 | 33 | |
|
34 | 34 | Return the *USER* CPU time in seconds since the start of the process. |
|
35 | 35 | This is done via a call to resource.getrusage, so it avoids the |
|
36 | 36 | wraparound problems in time.clock().""" |
|
37 | 37 | |
|
38 | 38 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
39 | 39 | |
|
40 | 40 | def clocks(): |
|
41 | 41 | """clocks() -> floating point number |
|
42 | 42 | |
|
43 | 43 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
44 | 44 | This is done via a call to resource.getrusage, so it avoids the |
|
45 | 45 | wraparound problems in time.clock().""" |
|
46 | 46 | |
|
47 | 47 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
48 | 48 | |
|
49 | 49 | def clock(): |
|
50 | 50 | """clock() -> floating point number |
|
51 | 51 | |
|
52 | 52 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
53 | 53 | the process. This is done via a call to resource.getrusage, so it |
|
54 | 54 | avoids the wraparound problems in time.clock().""" |
|
55 | 55 | |
|
56 | 56 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
57 | 57 | return u+s |
|
58 | 58 | |
|
59 | 59 | def clock2(): |
|
60 | 60 | """clock2() -> (t_user,t_system) |
|
61 | 61 | |
|
62 | 62 | Similar to clock(), but return a tuple of user/system times.""" |
|
63 | 63 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
64 | except ImportError: | |
|
64 | else: | |
|
65 | 65 | # There is no distinction of user/system time under windows, so we just use |
|
66 | 66 | # time.perff_counter() for everything... |
|
67 | 67 | clocku = clocks = clock = time.perf_counter |
|
68 | 68 | def clock2(): |
|
69 | 69 | """Under windows, system CPU time can't be measured. |
|
70 | 70 | |
|
71 | 71 | This just returns perf_counter() and zero.""" |
|
72 | 72 | return time.perf_counter(),0.0 |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | def timings_out(reps,func,*args,**kw): |
|
76 | 76 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
77 | 77 | |
|
78 | 78 | Execute a function reps times, return a tuple with the elapsed total |
|
79 | 79 | CPU time in seconds, the time per call and the function's output. |
|
80 | 80 | |
|
81 | 81 | Under Unix, the return value is the sum of user+system time consumed by |
|
82 | 82 | the process, computed via the resource module. This prevents problems |
|
83 | 83 | related to the wraparound effect which the time.clock() function has. |
|
84 | 84 | |
|
85 | 85 | Under Windows the return value is in wall clock seconds. See the |
|
86 | 86 | documentation for the time module for more details.""" |
|
87 | 87 | |
|
88 | 88 | reps = int(reps) |
|
89 | 89 | assert reps >=1, 'reps must be >= 1' |
|
90 | 90 | if reps==1: |
|
91 | 91 | start = clock() |
|
92 | 92 | out = func(*args,**kw) |
|
93 | 93 | tot_time = clock()-start |
|
94 | 94 | else: |
|
95 | 95 | rng = range(reps-1) # the last time is executed separately to store output |
|
96 | 96 | start = clock() |
|
97 | 97 | for dummy in rng: func(*args,**kw) |
|
98 | 98 | out = func(*args,**kw) # one last time |
|
99 | 99 | tot_time = clock()-start |
|
100 | 100 | av_time = tot_time / reps |
|
101 | 101 | return tot_time,av_time,out |
|
102 | 102 | |
|
103 | 103 | |
|
104 | 104 | def timings(reps,func,*args,**kw): |
|
105 | 105 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
106 | 106 | |
|
107 | 107 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
108 | 108 | time in seconds and the time per call. These are just the first two values |
|
109 | 109 | in timings_out().""" |
|
110 | 110 | |
|
111 | 111 | return timings_out(reps,func,*args,**kw)[0:2] |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | def timing(func,*args,**kw): |
|
115 | 115 | """timing(func,*args,**kw) -> t_total |
|
116 | 116 | |
|
117 | 117 | Execute a function once, return the elapsed total CPU time in |
|
118 | 118 | seconds. This is just the first value in timings_out().""" |
|
119 | 119 | |
|
120 | 120 | return timings_out(1,func,*args,**kw)[0] |
|
121 | 121 |
General Comments 0
You need to be logged in to leave comments.
Login now