Last active
June 28, 2019 13:29
-
-
Save jetpacktuxedo/a2d4ef619b580eedc1d8 to your computer and use it in GitHub Desktop.
Performance improvements for python used at http://blog.carlesmateo.com/2014/10/13/performance-of-several-languages/
This file contains 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
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