from util import addattr def f1(n): total = 0 for i in range(n): total += i return total def f2(n): try: total = 0 for i in range(n): total += i return total except: raise # remark: # f1 and f2 are both not inlinable, because they contain loops. def f3(n): try: return f1(n) except: raise def wrap(f): def wrapper(n): try: return f(n) except: raise return wrapper f4 = wrap(f1) def wrap2(f): def wrapper(*args, **kwds): try: return f(*args, **kwds) except: raise return wrapper f5 = wrap2(f1) # remark: # f4 and f5 both can not be compiled, because they use a cell variable. # the timing is actually not dependent of keyword args, because # the cell/free variables disable compilation @addattr(reps=5000) def time_inline_no_exceptions(n): total = 0 for i in range(n): total += f1(i) @addattr(reps=5000) def time_inline_exceptions(n): total = 0 for i in range(n): total += f2(i) # remark: # inlining does not occour above. # The good speed in both cases comes from the fact that psyco is # always exception aware. But because no exception is triggered, # there is no speed difference. @addattr(reps=5000) def time_wrapped_exceptions(n): total = 0 for i in range(n): total += f3(i) # remark: # time_wrapped_exceptions calls f3, which does not get inlined. # But f3 calls f1 once, which contains a very fast loop. @addattr(reps=2500) def time_decorator_nokwds(n): total = 0 for i in range(n): total += f4(i) @addattr(reps=2500) def time_decorator_kwds(n): total = 0 for i in range(n): total += f5(i) # remark: # A decorator itself does not necessarily cost speed; it totally # depends on what the decorator does. (see @addattr which is a decorator # that costs nothing). # I think the effect of wrapping a function was meant here, but the wrapper # was not compilable, due to the use of free/cell. # BTW. I want to support cell variables. # For now, the work around would be to bind the extra local to a default # value. This will cause more problems, because producing a new function # more than 5 times will make the function megamorphic ... # (needs some discussion)