Created
February 21, 2022 05:11
-
-
Save Magnus167/54c330af2a899aac2be474593e5a76d3 to your computer and use it in GitHub Desktop.
timing loop addition methods in python
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 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