1

I work with Jupyter/IPython quite a lot and am used to including %%time magic command in front of each cell to get a hint of the amount of CPU and wall clock time spent.

Is there a simple way to obtain the same measurements from within a script?

More specifically, I'd like to have something like that:

 results = measure_times(something_possibly_parallel)
 print results.wall_clock, results.cpu_user, results.cpu_total
KT.
  • 10,815
  • 4
  • 47
  • 71

1 Answers1

0

You can implement the desired measure_times() utility as a python context manager. Then the usage will be as below:

with measure_times("block name"):
    your code here

A sample implementation measuring only wall time follows:

$ cat measure_times.py 
import timeit

class measure_times:
    def __init__(self, blockname):
        self.blockname = blockname

    def __enter__(self):
        self.starttime = timeit.default_timer()

    def __exit__(self, exc_type, exc_value, traceback):
        t = timeit.default_timer() - self.starttime
        print('--- Time measurement report: {}: {}s'.format(self.blockname, t))


with measure_times("Integer sum"):
    s = 0
    for i in range(1000000):
        s += i
    print(s)

with measure_times("Floating point sum"):
    s = 0
    for i in range(1000000):
        s += i
    print(s)

$ python measure_times.py 
499999500000
--- Time measurement report: Integer sum: 0.0683062076569s
499999500000
--- Time measurement report: Floating point sum: 0.066034078598s
Leon
  • 31,443
  • 4
  • 72
  • 97
  • Right. But I was also wondering about the way IPython manages to measure actual CPU time (which may be different from wall clock time in case of parallelism. The timeit module can't do that, can it? – KT. May 12 '17 at 16:42
  • No `timeit` doesn't measure CPU time. For measuring CPU time, refer to [this answer](https://codereview.stackexchange.com/a/48459) (though it will work only in Python 3.3 or newer). – Leon May 13 '17 at 14:44