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