Skip to content

Instantly share code, notes, and snippets.

@logston
Last active April 9, 2016 07:24
Show Gist options
  • Select an option

  • Save logston/2d72de89b5a2d5f8ad67497d88cb6d63 to your computer and use it in GitHub Desktop.

Select an option

Save logston/2d72de89b5a2d5f8ad67497d88cb6d63 to your computer and use it in GitHub Desktop.
I didn't expect the decorated version to execute as fast as it did. Only twice the duration as the "normal" version!
import math
# x = (- b + sqrt(b**2 - 4 ac)) / 2a
def div_2a(func):
def inner(a, b, c):
x = func(a, b, c)
x /= 2 * a
return x
return inner
def plus_neg_b(func):
def inner(a, b, c):
x = func(a, b, c)
x += -1 * b
return x
return inner
def sqrt(func):
def inner(a, b, c):
x = func(a, b, c)
x = math.sqrt(x)
return x
return inner
def sub_4ac(func):
def inner(a, b, c):
x = func(a, b, c)
x -= 4 * a * c
return x
return inner
def square_b(func):
def inner(a, b, c):
return b ** 2
return inner
@div_2a
@plus_neg_b
@sqrt
@sub_4ac
@square_b
def main(a, b, c):
pass
def simple(a, b, c):
return (-1 * b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
if __name__ == '__main__':
print(main(9, 12, 4))
➜ ~ python -m timeit -s 'import decorate' 'decorate.main(9, 12, 4)'
1000000 loops, best of 3: 1.53 usec per loop
➜ ~ python -m timeit -s 'import decorate' 'decorate.simple(9, 12, 4)'
1000000 loops, best of 3: 0.843 usec per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment