Skip to content

Instantly share code, notes, and snippets.

@rxa254
Last active April 19, 2020 03:20
Show Gist options
  • Save rxa254/43df74f30d10570c51ea4cdbd44d7a6a to your computer and use it in GitHub Desktop.
Save rxa254/43df74f30d10570c51ea4cdbd44d7a6a to your computer and use it in GitHub Desktop.
How to time parts of your python code

HowTo Time some Piece O' Code =========== I always forget how I should time little parts of my python code when I'm trying to optimize it, so I put it here so that I can just search for it later.

There's a bunch of timing functions in python: Some use various different clocks, but the Stack Exchange wisdom makes me want to use the timeit module.

Here's how its done ----.. codeblock:: python

from timeit import default_timer as timer

t_0 = timer() # here's some code b = 2 + 2 t_elapsed = timer() - t_0 print('Elapsed time = {t:4.4f} seconds.'.format(t=t_elapsed))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment