Skip to content

Instantly share code, notes, and snippets.

@hellpanderrr
Last active August 29, 2015 14:22
Show Gist options
  • Save hellpanderrr/96ca448054ad65ca293f to your computer and use it in GitHub Desktop.
Save hellpanderrr/96ca448054ad65ca293f to your computer and use it in GitHub Desktop.
Print time needed for code execution
import time
class print_time:
    def __enter__(self):
        self.time = time.time()
        return self.time
    def __exit__(self, type, value, traceback):
        time_elapsed = time.time() - self.time
        print time_elapsed

with print_time() as _:
    do_something()
# '_' holds start time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment