Time simple sum¶
In [20]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy.random import default_rng
from time import time
In [21]:
## write a function which sums by looping
def mysum(x) -> float:
'''sum x with loop'''
temp: float = 0.0
for v in x:
temp += v
return temp
In [22]:
## big array that we will sum
n = 1000000 #size of array
rng = default_rng(34)
barray = rng.random(n) ## iid U(0,1)
print(pd.Series(barray).describe())
count 1.000000e+06 mean 5.000947e-01 std 2.886526e-01 min 4.442055e-07 25% 2.497438e-01 50% 5.003795e-01 75% 7.500236e-01 max 9.999996e-01 dtype: float64
In [23]:
## plot
nplt = 100 #number draws to plot
plt.scatter(np.arange(nplt),barray[:nplt])
plt.xlabel("draw number"); plt.ylabel("draw")
plt.title("iid U(0,1)")
Out[23]:
Text(0.5, 1.0, 'iid U(0,1)')
In [24]:
## check the same answer from different approaches to computing the sum
s0 = sum(barray)
s1 = mysum(barray)
s2 = barray.sum()
s3 = np.sum(barray)
print(f'\n** the four are {s0}, {s1}, {s2}, and {s3}')
** the four are 500094.70797370595, 500094.70797370595, 500094.7079736952, and 500094.7079736952
In [25]:
## time
t1 = time()
temp = mysum(barray)
t2 = time()
print(f'the time for mysum is {t2-t1}')
t1 = time()
temp = barray.sum()
t2 = time()
print(f'the time for the sum method is {t2-t1}')
the time for mysum is 0.052706241607666016 the time for the sum method is 0.0005590915679931641
In [26]:
0.055121421813964844/0.0005795955657958984
Out[26]:
95.10324969148499
In [27]:
## time the different approaches
print("python sum function")
%timeit sum(barray)
print("numpy sum function")
%timeit np.sum(barray)
print("ndarray sum method")
%timeit barray.sum()
print("mysum function with loop")
%timeit mysum(barray)
python sum function 41.4 ms ± 16 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) numpy sum function 217 µs ± 357 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each) ndarray sum method 215 µs ± 718 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each) mysum function with loop 53.3 ms ± 595 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)