Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/utils/timing.py: 35%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# encoding: utf-8
2"""
3Utilities for timing code execution.
4"""
6#-----------------------------------------------------------------------------
7# Copyright (C) 2008-2011 The IPython Development Team
8#
9# Distributed under the terms of the BSD License. The full license is in
10# the file COPYING, distributed as part of this software.
11#-----------------------------------------------------------------------------
13#-----------------------------------------------------------------------------
14# Imports
15#-----------------------------------------------------------------------------
17from __future__ import annotations
19import time
20from typing import Any, Callable
22#-----------------------------------------------------------------------------
23# Code
24#-----------------------------------------------------------------------------
26# If possible (Unix), use the resource module instead of time.clock()
27try:
28 import resource
29except ModuleNotFoundError:
30 resource = None # type: ignore [assignment]
32# Some implementations (like jyputerlite) don't have getrusage
33if resource is not None and hasattr(resource, "getrusage"):
34 def clocku():
35 """clocku() -> floating point number
37 Return the *USER* CPU time in seconds since the start of the process.
38 This is done via a call to resource.getrusage, so it avoids the
39 wraparound problems in time.clock()."""
41 return resource.getrusage(resource.RUSAGE_SELF)[0]
43 def clocks():
44 """clocks() -> floating point number
46 Return the *SYSTEM* CPU time in seconds since the start of the process.
47 This is done via a call to resource.getrusage, so it avoids the
48 wraparound problems in time.clock()."""
50 return resource.getrusage(resource.RUSAGE_SELF)[1]
52 def clock():
53 """clock() -> floating point number
55 Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
56 the process. This is done via a call to resource.getrusage, so it
57 avoids the wraparound problems in time.clock()."""
59 u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
60 return u+s
62 def clock2() -> tuple[float, float]:
63 """clock2() -> (t_user,t_system)
65 Similar to clock(), but return a tuple of user/system times."""
66 return resource.getrusage(resource.RUSAGE_SELF)[:2]
68else:
69 # There is no distinction of user/system time under windows, so we just use
70 # time.process_time() for everything...
71 clocku = clocks = clock = time.process_time
73 def clock2() -> tuple[float, float]:
74 """Under windows, system CPU time can't be measured.
76 This just returns process_time() and zero."""
77 return time.process_time(), 0.0
80def timings_out(
81 reps: int,
82 func: Callable[..., Any],
83 *args: Any,
84 **kw: Any,
85) -> tuple[float, float, Any]:
86 """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
88 Execute a function reps times, return a tuple with the elapsed total
89 CPU time in seconds, the time per call and the function's output.
91 Under Unix, the return value is the sum of user+system time consumed by
92 the process, computed via the resource module. This prevents problems
93 related to the wraparound effect which the time.clock() function has.
95 Under Windows the return value is in wall clock seconds. See the
96 documentation for the time module for more details."""
98 reps = int(reps)
99 assert reps >=1, 'reps must be >= 1'
100 if reps==1:
101 start = clock()
102 out = func(*args,**kw)
103 tot_time = clock()-start
104 else:
105 rng = range(reps-1) # the last time is executed separately to store output
106 start = clock()
107 for dummy in rng: func(*args,**kw)
108 out = func(*args,**kw) # one last time
109 tot_time = clock()-start
110 av_time = tot_time / reps
111 return tot_time,av_time,out
114def timings(
115 reps: int,
116 func: Callable[..., Any],
117 *args: Any,
118 **kw: Any,
119) -> tuple[float, float]:
120 """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
122 Execute a function reps times, return a tuple with the elapsed total CPU
123 time in seconds and the time per call. These are just the first two values
124 in timings_out()."""
126 return timings_out(reps,func,*args,**kw)[0:2]
129def timing(func: Callable[..., Any], *args: Any, **kw: Any) -> float:
130 """timing(func,*args,**kw) -> t_total
132 Execute a function once, return the elapsed total CPU time in
133 seconds. This is just the first value in timings_out()."""
135 return timings_out(1,func,*args,**kw)[0]