48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
|
#+
|
||
|
# ARES/HADES/BORG Package -- ./experiments/CIC/timing.py
|
||
|
# Copyright (C) 2014-2020 Guilhem Lavaux <guilhem.lavaux@iap.fr>
|
||
|
# Copyright (C) 2009-2020 Jens Jasche <jens.jasche@fysik.su.se>
|
||
|
#
|
||
|
# Additional contributions from:
|
||
|
# Guilhem Lavaux <guilhem.lavaux@iap.fr> (2023)
|
||
|
#
|
||
|
#+
|
||
|
import time
|
||
|
from contextlib import contextmanager
|
||
|
|
||
|
@contextmanager
|
||
|
def time_block(name):
|
||
|
ts = time.time()
|
||
|
yield
|
||
|
te = time.time()
|
||
|
|
||
|
print '%s %2.2f sec' % \
|
||
|
(name, te-ts)
|
||
|
|
||
|
def timeit(method):
|
||
|
|
||
|
def timed(*args, **kw):
|
||
|
ts = time.time()
|
||
|
result = method(*args, **kw)
|
||
|
te = time.time()
|
||
|
|
||
|
print '%r (%r, %r) %2.2f sec' % \
|
||
|
(method.__name__, args, kw, te-ts)
|
||
|
return result
|
||
|
|
||
|
return timed
|
||
|
|
||
|
def timeit_quiet(method):
|
||
|
|
||
|
def timed(*args, **kw):
|
||
|
ts = time.time()
|
||
|
result = method(*args, **kw)
|
||
|
te = time.time()
|
||
|
|
||
|
print '%r %2.2f sec' % \
|
||
|
(method.__name__, te-ts)
|
||
|
return result
|
||
|
|
||
|
return timed
|
||
|
|