Skip to content

Instantly share code, notes, and snippets.

@tateg
Created December 16, 2021 01:06
Show Gist options
  • Save tateg/f0faea4a8d92748983ba0ff257160163 to your computer and use it in GitHub Desktop.
Save tateg/f0faea4a8d92748983ba0ff257160163 to your computer and use it in GitHub Desktop.
Basic Python loop timing with print statements in the loop
from time import time_ns
before_ts = time_ns()
for num in range(0, 1000):
print(num)
after_ts = time_ns()
diff = after_ts - before_ts
print('loop time in \n' \
'nanoseconds: ' + str(diff) + '\n' + \
'microseconds: ' + str(diff / 1000) + '\n' + \
'milliseconds: ' + str((diff / 1000) / 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment