Last active
August 20, 2018 23:10
-
-
Save henriquerichter/fd4ceb7426d1173e2e864c8ff109d474 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
| import time | |
| class Benchmark(): | |
| def __init__(self, txt): | |
| self.txt = str(txt) | |
| self.start = time.time() | |
| def __enter__(self): | |
| return self | |
| def __exit__(self, exc_type, exc_val, exc_tb): | |
| end = time.time() | |
| print(self.txt + " took " + str(end - self.start) + " seconds.") | |
| def example_function(): | |
| time.sleep(5) | |
| if __name__ == '__main__': | |
| with Benchmark("example_function"): | |
| example_function() |
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 time | |
| def benchmark(func): | |
| def _timer(*args, **kwargs): | |
| start = time.time() | |
| value = func(*args, **kwargs) | |
| end = time.time() | |
| print(str(func.__name__) + " took " + str(end - start) + " seconds.") | |
| return value | |
| return _timer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment