Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Created October 13, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perfecto25/c70bb17c7dbdbdaeea435ea07719622d to your computer and use it in GitHub Desktop.
Save perfecto25/c70bb17c7dbdbdaeea435ea07719622d to your computer and use it in GitHub Desktop.
Py benchmarks
from functools import reduce
import time
numbers = list(range(51000000))
def add(numbers):
total = 0
for n in numbers:
total += n
return total
print("\nusing Function")
start = time.time()
print(f"total: {add(numbers)}")
end = time.time()
print(f"time in sec: {end-start}")
print("\nusing builtin Sum funcion")
start = time.time()
print(f"total: {sum(numbers)}")
end = time.time()
print(f"time in sec: {end-start}")
print("\nusing lambda")
start = time.time()
print(f"total: {reduce(lambda x,y: x+y, numbers)}")
end = time.time()
print(f"time in sec: {end-start}")
print("\nusing lambda with dunder method")
start = time.time()
print(f"total: {reduce(lambda x,y: x.__add__(y), numbers)}")
end = time.time()
print(f"time in sec: {end-start}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment