diff --git a/README.md b/README.md index 4d47abcb3f502f5082af332ead8139d8930e2b04..47f0a8456fe05ebe775486f7c68e25a0c4b73d4a 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,4 @@ Tools for working with NREL's FAST code (An aeroelastic computer-aided engineeri Other functions - [geometry](wetb/functions/geometry.py): Different kind of geometry conversion functions - [process_exec](wetb/functions/process_exec.py): Run system command in subprocess +- [timing](wetb/functions/timing.py): Decorators for evaluating execution time of functions diff --git a/wetb/functions/timing.py b/wetb/functions/timing.py index feabe52c5e8d36b75267c8294403ff95cb45e71b..f3aea9a345a8725e7e75ffc1f8b3f33c92106679 100644 --- a/wetb/functions/timing.py +++ b/wetb/functions/timing.py @@ -2,6 +2,17 @@ from six import exec_ import time import inspect def get_time(f): + """Get time decorator + returns (return_values, time_of_execution) + + >>> @get_time + >>> def test(): + >>> time.sleep(1) + >>> return "end" + >>> + >>> test() + ('end', 0.999833492421551) + """ def wrap(*args, **kwargs): t = time.clock() res = f(*args, **kwargs) @@ -12,56 +23,94 @@ def get_time(f): def print_time(f): - def wrap(*args, **kwargs): - t = time.time() - res = f(*args, **kwargs) - print ("%-12s\t%.3fs" % (f.__name__, time.time() - t)) - return res - w = wrap - w.__name__ = f.__name__ - return w + """Print time decorator + prints name of method and time of execution + + >>> @print_time + >>> def test(): + >>> time.sleep(1) + >>> + >>> test() + test 1.000s + """ + def wrap(*args, **kwargs): + t = time.time() + res = f(*args, **kwargs) + print ("%-12s\t%.3fs" % (f.__name__, time.time() - t)) + return res + w = wrap + w.__name__ = f.__name__ + return w cum_time = {} def print_cum_time(f): - if f not in cum_time: - cum_time[f] = (0, 0) - - def wrap(*args, **kwargs): - t = time.time() - res = f(*args, **kwargs) - ct = cum_time[f][1] + time.time() - t - n = cum_time[f][0] + 1 - cum_time[f] = (n, ct) - print ("%-12s\t%.4d calls, %03fs, %fs pr. call'" % (f.__name__, n, ct, ct / n)) - return res - w = wrap - w.__name__ = f.__name__ - return w + """Print cumulated time decorator + prints name of method and cumulated time of execution + + >>> @print_cum_time + >>> def test(): + >>> time.sleep(1) + >>> + >>> test() + test 0001 calls, 1.000000s, 1.000000s pr. call' + >>> test() + test 0002 calls, 2.000000s, 1.000000s pr. call' + """ + if f not in cum_time: + cum_time[f] = (0, 0) + + def wrap(*args, **kwargs): + t = time.time() + res = f(*args, **kwargs) + ct = cum_time[f][1] + time.time() - t + n = cum_time[f][0] + 1 + cum_time[f] = (n, ct) + print ("%-12s\t%.4d calls, %03fs, %fs pr. call'" % (f.__name__, n, ct, ct / n)) + return res + w = wrap + w.__name__ = f.__name__ + return w def print_line_time(f): - def wrap(*args, **kwargs): - arg_names, varargs, varkw, defaults = inspect.getargspec(f) - kwargs[varargs] = args[len(arg_names):] - kwargs[varkw] = {} - for k, v in kwargs.items(): - if k not in tuple(arg_names) + (varargs, varkw): - kwargs.pop(k) - kwargs[varkw][k] = v - if defaults: - kwargs.update(dict(zip(arg_names[::-1], defaults[::-1]))) - kwargs.update(dict(zip(arg_names, args))) - - - lines = inspect.getsourcelines(f)[0][2:] - tcum = time.clock() - locals = kwargs - gl = f.__globals__ - - for l in lines: - tline = time.clock() - exec(l.strip(), locals, gl) #res = f(*args, **kwargs) - print ("%.3fs\t%.3fs\t%s" % (time.clock() - tline, time.clock() - tcum, l.strip())) - w = wrap - w.__name__ = f.__name__ - return w + """Execute one line at the time and prints the time of execution. + Only for non-branching and non-looping code + + prints: time_of_line, cumulated_time, code_line + + + >>> @print_line_time + >>> def test(): + >>> time.sleep(.3) + >>> time.sleep(.5) + >>> + >>> test() + 0.300s 0.300s time.sleep(.3) + 0.510s 0.810s time.sleep(.51) + + """ + def wrap(*args, **kwargs): + arg_names, varargs, varkw, defaults = inspect.getargspec(f) + kwargs[varargs] = args[len(arg_names):] + kwargs[varkw] = {} + for k, v in kwargs.items(): + if k not in tuple(arg_names) + (varargs, varkw): + kwargs.pop(k) + kwargs[varkw][k] = v + if defaults: + kwargs.update(dict(zip(arg_names[::-1], defaults[::-1]))) + kwargs.update(dict(zip(arg_names, args))) + + + lines = inspect.getsourcelines(f)[0][2:] + tcum = time.clock() + locals = kwargs + gl = f.__globals__ + + for l in lines: + tline = time.clock() + exec(l.strip(), locals, gl) #res = f(*args, **kwargs) + print ("%.3fs\t%.3fs\t%s" % (time.clock() - tline, time.clock() - tcum, l.strip())) + w = wrap + w.__name__ = f.__name__ + return w diff --git a/wetb/hawc2/htc_file.py b/wetb/hawc2/htc_file.py index b24dc343fe7079747e59061f6e31674ef41023c5..50d834be0b11bcad06483bd63a22e4af9e95ce27 100644 --- a/wetb/hawc2/htc_file.py +++ b/wetb/hawc2/htc_file.py @@ -2,6 +2,9 @@ Created on 20/01/2014 @author: MMPE + +See documentation of HTCFile below + ''' from collections import OrderedDict import os