Skip to content

Instantly share code, notes, and snippets.

@e-dreyer
Created August 10, 2023 07:20
Show Gist options
  • Save e-dreyer/dca95637083e979f53d4fac03eae9e3a to your computer and use it in GitHub Desktop.
Save e-dreyer/dca95637083e979f53d4fac03eae9e3a to your computer and use it in GitHub Desktop.
Python Itertools experiments
from itertools import accumulate
from timeit import default_timer as timer
if __name__ == "__main__":
custom_accumulate_start = timer()
myList = range(0, 10000, 2)
myTotal = 0
myAccumulation = list()
for myItem in myList:
myTotal += int(myItem)
myAccumulation.append(myTotal)
custom_accumulate_end = timer()
itertools_accumulate_start = timer()
iterAccumulation = list(accumulate(myList))
itertools_accumulate_end = timer()
custom_accumulate_duration = custom_accumulate_end-custom_accumulate_start
itertools_accumulate_duration = itertools_accumulate_end-itertools_accumulate_start
difference = (itertools_accumulate_duration - custom_accumulate_duration)
improvement = (difference/itertools_accumulate_duration) * 100
assert myAccumulation == iterAccumulation
print(f'custom accumulation result in {custom_accumulate_duration} seconds')
print(f'itertools accumulation result in {itertools_accumulate_duration} seconds')
print(f'{(difference/itertools_accumulate_duration) * 100}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment