|
|
@ -16,6 +16,7 @@ Simple timer utility. |
|
|
|
This module is part of wpylib project. |
|
|
|
This module is part of wpylib project. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
|
import time |
|
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
class timer: |
|
|
|
class timer: |
|
|
@ -38,6 +39,12 @@ class block_timer(object): |
|
|
|
|
|
|
|
|
|
|
|
with block_timer() as t: |
|
|
|
with block_timer() as t: |
|
|
|
<code> |
|
|
|
<code> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fancy options are available to record the timing result, specified when |
|
|
|
|
|
|
|
initializing the new object: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* report=block_timer.writeout_file :: writes out the timing result to a file |
|
|
|
|
|
|
|
* report=block_timer.writeout_dict :: saves the timing result to a dict-like element |
|
|
|
""" |
|
|
|
""" |
|
|
|
@staticmethod |
|
|
|
@staticmethod |
|
|
|
def writeout_file(out): |
|
|
|
def writeout_file(out): |
|
|
@ -49,21 +56,23 @@ class block_timer(object): |
|
|
|
return wrt_dict |
|
|
|
return wrt_dict |
|
|
|
#return lambda tm: rec.__setitem__(key, tm) |
|
|
|
#return lambda tm: rec.__setitem__(key, tm) |
|
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
@classmethod |
|
|
|
def bt_file(fobj): |
|
|
|
def bt_file(cls, fobj): |
|
|
|
|
|
|
|
"""An object factory to report the timing to a text file. |
|
|
|
|
|
|
|
""" |
|
|
|
if isinstance(fobj, basestring): |
|
|
|
if isinstance(fobj, basestring): |
|
|
|
from wpylib.iofmt.text_output import text_output |
|
|
|
from wpylib.iofmt.text_output import text_output |
|
|
|
out = text_output(fobj) |
|
|
|
out = text_output(fobj) |
|
|
|
else: |
|
|
|
else: |
|
|
|
out = fobj |
|
|
|
out = fobj |
|
|
|
return block_timer(report=block_timer.writeout_file(out)) |
|
|
|
return cls(report=block_timer.writeout_file(out)) |
|
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
@classmethod |
|
|
|
def bt_dict(rec, key): |
|
|
|
def bt_dict(cls, rec, key): |
|
|
|
return block_timer(report=block_timer.writeout_dict(rec, key)) |
|
|
|
return cls(report=block_timer.writeout_dict(rec, key)) |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, report=None): |
|
|
|
def __init__(self, report=None): |
|
|
|
if report == None: report = block_timer.writeout_file(sys.stdout) |
|
|
|
if report is None: report = block_timer.writeout_file(sys.stdout) |
|
|
|
self.report = report |
|
|
|
self.report = report |
|
|
|
|
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
def __enter__(self): |
|
|
|