Last active
April 9, 2016 07:24
-
-
Save logston/2d72de89b5a2d5f8ad67497d88cb6d63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I didn't expect the decorated version to execute as fast as it did. Only twice the duration as the "normal" version! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ➜ ~ 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