Skip to content

Instantly share code, notes, and snippets.

@Magnus167
Created February 21, 2022 05:11
Show Gist options
  • Save Magnus167/54c330af2a899aac2be474593e5a76d3 to your computer and use it in GitHub Desktop.
Save Magnus167/54c330af2a899aac2be474593e5a76d3 to your computer and use it in GitHub Desktop.
timing loop addition methods in python
from random import SystemRandom as SR
def f1(arr):
s = 0
for i in range(len(arr)):
for j in range(len(arr[i])):
s += arr[i][j]
return s
def f2(arr):
s = 0
for i in range(len(arr)):
for j in range(len(arr[i])):
s += arr[j][i]
return s
def f3(arr):
s = 0
for i in range(len(arr)):
s += sum(arr[i])
return s
def f4(arr):
s = sum([sum(i) for i in arr])
return s
arr = [[SR().randint(0, 100) for _ in range(1000)] for _ in range(1000)]
timeit f1(arr)
timeit f2(arr)
timeit f3(arr)
timeit f4(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment