-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_timeit.py
More file actions
34 lines (27 loc) · 839 Bytes
/
Copy pathloop_timeit.py
File metadata and controls
34 lines (27 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from timeit import default_timer
arr = [[0] * 1000] * 1000
res = 0
ts = default_timer()
for i in range(1000):
for j in range(1000):
if arr[i][j] > 0:
res += 1
print(f"took: {default_timer() - ts}s")
ts = default_timer()
for i in range(1000):
for j in range(1000):
if arr[j][i] > 0:
res += 1
print(f"took: {default_timer() - ts}s")
ts = default_timer()
_ = [(res := res + 1) for i in range(1000)
for j in range(1000) if arr[i][j] > 0]
print(f"took: {default_timer() - ts}s")
ts = default_timer()
_ = [(res := res + 1) for i in range(1000)
for j in range(1000) if arr[j][i] > 0]
print(f"took: {default_timer() - ts}s")
ts = default_timer()
_ = [(res := res + 1) if arr[j][i] > 0 else res for i in range(1000)
for j in range(1000)]
print(f"took: {default_timer() - ts}s")