##// END OF EJS Templates
Backport PR #13050: Make Ipython.utils.timing work with jupyterlite
Matthias Bussonnier -
Show More
@@ -1,116 +1,123 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 except ImportError:
27 resource = None
28
29 # Some implementations (like jyputerlite) don't have getrusage
30 if resource is not None and hasattr(resource, "getrusage"):
26 31 def clocku():
27 32 """clocku() -> floating point number
28 33
29 34 Return the *USER* CPU time in seconds since the start of the process.
30 35 This is done via a call to resource.getrusage, so it avoids the
31 36 wraparound problems in time.clock()."""
32 37
33 38 return resource.getrusage(resource.RUSAGE_SELF)[0]
34 39
35 40 def clocks():
36 41 """clocks() -> floating point number
37 42
38 43 Return the *SYSTEM* CPU time in seconds since the start of the process.
39 44 This is done via a call to resource.getrusage, so it avoids the
40 45 wraparound problems in time.clock()."""
41 46
42 47 return resource.getrusage(resource.RUSAGE_SELF)[1]
43 48
44 49 def clock():
45 50 """clock() -> floating point number
46 51
47 52 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
48 53 the process. This is done via a call to resource.getrusage, so it
49 54 avoids the wraparound problems in time.clock()."""
50 55
51 56 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
52 57 return u+s
53 58
54 59 def clock2():
55 60 """clock2() -> (t_user,t_system)
56 61
57 62 Similar to clock(), but return a tuple of user/system times."""
58 63 return resource.getrusage(resource.RUSAGE_SELF)[:2]
59 except ImportError:
64
65
66 else:
60 67 # There is no distinction of user/system time under windows, so we just use
61 68 # time.perff_counter() for everything...
62 69 clocku = clocks = clock = time.perf_counter
63 70 def clock2():
64 71 """Under windows, system CPU time can't be measured.
65 72
66 73 This just returns perf_counter() and zero."""
67 74 return time.perf_counter(),0.0
68 75
69 76
70 77 def timings_out(reps,func,*args,**kw):
71 78 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
72 79
73 80 Execute a function reps times, return a tuple with the elapsed total
74 81 CPU time in seconds, the time per call and the function's output.
75 82
76 83 Under Unix, the return value is the sum of user+system time consumed by
77 84 the process, computed via the resource module. This prevents problems
78 85 related to the wraparound effect which the time.clock() function has.
79 86
80 87 Under Windows the return value is in wall clock seconds. See the
81 88 documentation for the time module for more details."""
82 89
83 90 reps = int(reps)
84 91 assert reps >=1, 'reps must be >= 1'
85 92 if reps==1:
86 93 start = clock()
87 94 out = func(*args,**kw)
88 95 tot_time = clock()-start
89 96 else:
90 97 rng = range(reps-1) # the last time is executed separately to store output
91 98 start = clock()
92 99 for dummy in rng: func(*args,**kw)
93 100 out = func(*args,**kw) # one last time
94 101 tot_time = clock()-start
95 102 av_time = tot_time / reps
96 103 return tot_time,av_time,out
97 104
98 105
99 106 def timings(reps,func,*args,**kw):
100 107 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
101 108
102 109 Execute a function reps times, return a tuple with the elapsed total CPU
103 110 time in seconds and the time per call. These are just the first two values
104 111 in timings_out()."""
105 112
106 113 return timings_out(reps,func,*args,**kw)[0:2]
107 114
108 115
109 116 def timing(func,*args,**kw):
110 117 """timing(func,*args,**kw) -> t_total
111 118
112 119 Execute a function once, return the elapsed total CPU time in
113 120 seconds. This is just the first value in timings_out()."""
114 121
115 122 return timings_out(1,func,*args,**kw)[0]
116 123
General Comments 0
You need to be logged in to leave comments. Login now