##// END OF EJS Templates
- Modified clock() to return total (user+system) time. Introduced...
fperez -
Show More
@@ -5,7 +5,7 b' General purpose utilities.'
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
5 This is a grab-bag of stuff I find useful in most programs I write. Some of
6 these things are also convenient when working at the command line.
6 these things are also convenient when working at the command line.
7
7
8 $Id: genutils.py 1930 2006-11-26 17:22:13Z vivainio $"""
8 $Id: genutils.py 2108 2007-02-23 00:31:17Z fperez $"""
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
11 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
@@ -163,16 +163,34 b' StringTypes = types.StringTypes'
163 # If possible (Unix), use the resource module instead of time.clock()
163 # If possible (Unix), use the resource module instead of time.clock()
164 try:
164 try:
165 import resource
165 import resource
166 def clock():
166 def clocku():
167 """clock() -> floating point number
167 """clocku() -> floating point number
168
168
169 Return the CPU time in seconds (user time only, system time is
169 Return the *USER* CPU time in seconds since the start of the process.
170 ignored) since the start of the process. This is done via a call to
170 This is done via a call to resource.getrusage, so it avoids the
171 resource.getrusage, so it avoids the wraparound problems in
171 wraparound problems in time.clock()."""
172 time.clock()."""
173
172
174 return resource.getrusage(resource.RUSAGE_SELF)[0]
173 return resource.getrusage(resource.RUSAGE_SELF)[0]
175
174
175 def clocks():
176 """clocks() -> floating point number
177
178 Return the *SYSTEM* CPU time in seconds since the start of the process.
179 This is done via a call to resource.getrusage, so it avoids the
180 wraparound problems in time.clock()."""
181
182 return resource.getrusage(resource.RUSAGE_SELF)[1]
183
184 def clock():
185 """clock() -> floating point number
186
187 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
188 the process. This is done via a call to resource.getrusage, so it
189 avoids the wraparound problems in time.clock()."""
190
191 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
192 return u+s
193
176 def clock2():
194 def clock2():
177 """clock2() -> (t_user,t_system)
195 """clock2() -> (t_user,t_system)
178
196
@@ -180,7 +198,9 b' try:'
180 return resource.getrusage(resource.RUSAGE_SELF)[:2]
198 return resource.getrusage(resource.RUSAGE_SELF)[:2]
181
199
182 except ImportError:
200 except ImportError:
183 clock = time.clock
201 # There is no distinction of user/system time under windows, so we just use
202 # time.clock() for everything...
203 clocku = clocks = clock = time.clock
184 def clock2():
204 def clock2():
185 """Under windows, system CPU time can't be measured.
205 """Under windows, system CPU time can't be measured.
186
206
@@ -1,3 +1,14 b''
1 2007-02-22 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/genutils.py (clock): I modified clock() to return total
4 time, user+system. This is a more commonly needed metric. I also
5 introduced the new clocku/clocks to get only user/system time if
6 one wants those instead.
7
8 ***WARNING: API CHANGE*** clock() used to return only user time,
9 so if you want exactly the same results as before, use clocku
10 instead.
11
1 2007-02-22 Ville Vainio <vivainio@gmail.com>
12 2007-02-22 Ville Vainio <vivainio@gmail.com>
2
13
3 * IPython/Extensions/ipy_p4.py: Extension for improved
14 * IPython/Extensions/ipy_p4.py: Extension for improved
General Comments 0
You need to be logged in to leave comments. Login now