joonro
2/3/2015 - 10:41 PM

[Loop speed test] #python

[Loop speed test] #python

def summation(arg):
    return_val = 0
    
    for i in arg:
        return_val += i
    
    return(return_val)

test_range = np.arange(10)
%timeit summation(test_range)
100000 loops, best of 3: 2.22 µs per loop

%timeit test_range.sum()
100000 loops, best of 3: 2.45 µs per loop


test_range = np.arange(100)

%timeit summation(test_range)

%timeit test_range.sum()
100000 loops, best of 3: 13.4 µs per loop
100000 loops, best of 3: 2.45 µs per loop



test_range = np.arange(1000)

%timeit summation(test_range)
10000 loops, best of 3: 122 µs per loop

%timeit test_range.sum()
100000 loops, best of 3: 3.03 µs per loop



test_range = np.arange(10000)
%timeit summation(test_range)
1000 loops, best of 3: 1.24 ms per loop

%timeit test_range.sum()
100000 loops, best of 3: 6.59 µs per loop