xonsh.timings¶
Timing related functionality for the xonsh shell.
The following time_it alias and Timer was forked from the IPython project: * Copyright (c) 2008-2014, IPython Development Team * Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu> * Copyright (c) 2001, Janko Hauser <jhauser@zscout.de> * Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
- class xonsh.timings.Timer(stmt='pass', setup='pass', timer=<built-in function perf_counter>, globals=None)[source]¶
Timer class that explicitly uses self.inner which is an undocumented implementation detail of CPython, not shared by PyPy.
Constructor. See class doc string.
- autorange(callback=None)[source]¶
Return the number of loops and time taken so that total time >= 0.2.
Calls the timeit method with increasing numbers from the sequence 1, 2, 5, 10, 20, 50, … until the time taken is at least 0.2 second. Returns (number, time_taken).
If callback is given and is not None, it will be called after each trial with two arguments:
callback(number, time_taken).
- print_exc(file=None)[source]¶
Helper to print a traceback from the timed code.
Typical use:
t = Timer(…) # outside the try/except try:
t.timeit(…) # or t.repeat(…)
- except:
t.print_exc()
The advantage over the standard traceback is that source lines in the compiled template will be displayed.
The optional file argument directs where the traceback is sent; it defaults to sys.stderr.
- repeat(repeat=5, number=1000000)[source]¶
Call timeit() a few times.
This is a convenience function that calls the timeit() repeatedly, returning a list of results. The first argument specifies how many times to call timeit(), defaulting to 5; the second argument specifies the timer argument, defaulting to one million.
Note: it’s tempting to calculate mean and standard deviation from the result vector and report these. However, this is not very useful. In a typical case, the lowest value gives a lower bound for how fast your machine can run the given code snippet; higher values in the result vector are typically not caused by variability in Python’s speed, but by other processes interfering with your timing accuracy. So the min() of the result is probably the only number you should be interested in. After that, you should look at the entire vector and apply common sense rather than statistics.
- timeit(number=1000000)[source]¶
Time ‘number’ executions of the main statement. To be precise, this executes the setup statement once, and then returns the time it takes to execute the main statement a number of times, as a float measured in seconds. The argument is the number of times through the loop, defaulting to one million. The main statement, the setup statement and the timer function to be used are passed to the constructor.
- xonsh.timings.clock() floating point number[source]¶
Return the TOTAL USER+SYSTEM CPU time in seconds since the start of the process.
- xonsh.timings.clocks() floating point number[source]¶
Return the SYSTEM CPU time in seconds since the start of the process.
- xonsh.timings.clocku() floating point number[source]¶
Return the USER CPU time in seconds since the start of the process.