diff options
author | Zachary Sloan | 2013-02-28 23:37:59 +0000 |
---|---|---|
committer | Zachary Sloan | 2013-02-28 23:37:59 +0000 |
commit | fa4c4f116c37285d24edc9532bb223e18dfb1584 (patch) | |
tree | 99f076a0e5cd2a797957b6092344793caade6036 /wqflask/utility | |
parent | e82a11458cded705dd2056bad3cee76aa659288c (diff) | |
download | genenetwork2-fa4c4f116c37285d24edc9532bb223e18dfb1584.tar.gz |
Added benchmark.py that compares the time of various operations
and gives a report with the percent of total computation time
each takes
Diffstat (limited to 'wqflask/utility')
-rw-r--r-- | wqflask/utility/benchmark.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/wqflask/utility/benchmark.py b/wqflask/utility/benchmark.py new file mode 100644 index 00000000..0a6e422c --- /dev/null +++ b/wqflask/utility/benchmark.py @@ -0,0 +1,42 @@ +from __future__ import print_function, division, absolute_import + +import collections +import inspect +import time + + +class Bench(object): + entries = collections.OrderedDict() + def __init__(self, name=None): + self.name = name + + def __enter__(self): + if self.name: + print("Starting benchmark: %s" % (self.name)) + else: + print("Starting benchmark at: %s [%i]" % (inspect.stack()[1][3], inspect.stack()[1][2])) + self.start_time = time.time() + + def __exit__(self, type, value, traceback): + if self.name: + name = self.name + else: + name = "That" + + time_took = time.time() - self.start_time + print(" %s took: %f seconds" % (name, (time_took))) + + if self.name: + Bench.entries[self.name] = time_took + + @classmethod + def report(cls): + total_time = sum((time_took for time_took in cls.entries.itervalues())) + print("\nTiming report\n") + for name, time_took in cls.entries.iteritems(): + percent = int(round((time_took/total_time) * 100)) + print("[{}%] {}: {}".format(percent, name, time_took)) + print() + + # Reset the entries after reporting + cls.entries = collections.OrderedDict()
\ No newline at end of file |