Skip to content

Instantly share code, notes, and snippets.

@jetpacktuxedo
Last active June 28, 2019 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jetpacktuxedo/a2d4ef619b580eedc1d8 to your computer and use it in GitHub Desktop.
Save jetpacktuxedo/a2d4ef619b580eedc1d8 to your computer and use it in GitHub Desktop.
from datetime import datetime
import timeit
def loop():
i_counter = 0
# List from 0 to 31999, saved beforehand to not use the generator version of range
# Also reduces the overhead of generating the list twice, which other languages don't have to do
bigrange = list(range(32000))
# No need to save an index variable we never use
for _ in range(10):
for _ in bigrange:
for _ in bigrange:
i_counter = i_counter + 1 if i_counter < 50 else 0
print(i_counter)
print("Starting at: " + str(datetime.now()))
# Using timeit instead of computing the diference in times because it is more pythonic.
# Also coincidentally disables garbage collection
seconds = timeit.timeit('loop()', setup='from __main__ import loop', number=1)
print("Ending at: " + str(datetime.now()))
print("Total seconds:" + str(seconds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment