In [1]:
from random import randrange
arr = [randrange(10, 1000) for _ in range(100_000_000)]
In [2]:
arr[:10]
Out[2]:
[461, 499, 545, 908, 426, 376, 918, 944, 838, 77]
In [3]:
def sum_index(array):
suma = 0
for index in range(len(array)):
suma += array[index]
return suma
In [4]:
%timeit sum_index(arr)
5.69 s ± 32.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [5]:
def sum_enumerate(array):
suma = 0
for index, element in enumerate(array):
suma += element
return suma
In [6]:
%timeit sum_enumerate(arr)
6.1 s ± 68.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [10]:
def sum_in(array):
suma = 0
for element in array:
suma += element
return suma
In [8]:
%timeit sum_in(arr)
3.79 s ± 8.02 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [9]:
%timeit sum(arr)
2.41 s ± 41.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [4]:
from numba import jit
@jit
def sum_in_jit(array):
suma = 0
for element in array:
suma += element
return suma
In [8]:
%time sum_in_jit([1,2,3,4,5,6])
CPU times: total: 0 ns Wall time: 0 ns
Out[8]:
21
In [5]:
%time sum_in_jit(arr)
CPU times: total: 1min 46s Wall time: 1min 47s
Out[5]:
50450264134
In [6]:
from numba import jit, njit, prange
@njit(parallel=True)
def sum_index_prange(array):
suma = 0
for index in prange(len(array)):
suma += array[index]
return suma
In [7]:
%time sum_index_prange(arr)
CPU times: total: 1min 45s Wall time: 1min 45s
Out[7]:
50450264134
In [11]:
import numpy as np
pole= np.array(arr)
%time sum_in(pole)
C:\Users\student\AppData\Local\Temp\ipykernel_12064\3662415012.py:4: RuntimeWarning: overflow encountered in scalar add suma += element
CPU times: total: 7.38 s Wall time: 7.4 s
Out[11]:
-1089343418
In [12]:
import numpy as np
pole= np.array(arr)
%time np.sum(pole)
CPU times: total: 31.2 ms Wall time: 34.5 ms
Out[12]:
-1089343418
In [17]:
import numpy as np
%time pole= np.array(arr, dtype='int64')
%time sum_in(pole)
CPU times: total: 3.17 s Wall time: 3.18 s CPU times: total: 7.52 s Wall time: 7.51 s
Out[17]:
50450264134
In [18]:
%time np.sum(pole)
CPU times: total: 62.5 ms Wall time: 60.8 ms
Out[18]:
50450264134
In [33]:
#Generovanie pola priamo cez numpy
%time pole = np.random.randint(10, 1_000, size=100_000_000, dtype='int64')
%time np.sum(pole)
CPU times: total: 547 ms Wall time: 535 ms CPU times: total: 46.9 ms Wall time: 46.6 ms
Out[33]:
50446700629
In [34]:
%time sum_in(pole)
CPU times: total: 7.89 s Wall time: 7.94 s
Out[34]:
50446700629
In [35]:
%time sum_in_jit(pole)
CPU times: total: 250 ms Wall time: 255 ms
Out[35]:
50446700629
In [28]:
%time sum_in_jit(arr)
CPU times: total: 1min 46s Wall time: 1min 46s
Out[28]:
50450264134
Sum of two arrays¶
In [41]:
arr1 = np.random.randint(10, 1_000, size=100_000_000, dtype='int64')
arr2 = np.random.randint(10, 1_000, size=100_000_000, dtype='int64')
In [42]:
%time arr1+arr2
CPU times: total: 328 ms Wall time: 333 ms
Out[42]:
array([ 994, 1647, 537, ..., 578, 1000, 454], dtype=int64)
In [43]:
%time np.add(arr1, arr2)
CPU times: total: 297 ms Wall time: 298 ms
Out[43]:
array([ 994, 1647, 537, ..., 578, 1000, 454], dtype=int64)
In [51]:
from numba import jit, njit, prange
import numpy as np
@njit(parallel=True)
def sum_elements(array1, array2):
result = np.zeros(array1.shape[0])
for i in prange(array1.shape[0]):
result[i] = array1[i]+array2[i]
return result
%time sum_elements(arr1, arr2)
CPU times: total: 2.14 s Wall time: 495 ms
Out[51]:
array([ 994., 1647., 537., ..., 578., 1000., 454.])
In [55]:
%time sum_elements(arr1, arr2)
CPU times: total: 1.59 s Wall time: 187 ms
Out[55]:
array([ 994., 1647., 537., ..., 578., 1000., 454.])
Multiplication of two matices¶
In [92]:
arr1 = np.random.randint(10, 1_000, size=(100, 500), dtype='int64')
arr2 = np.random.randint(10, 1_000, size=(500, 300), dtype='int64')
In [93]:
arr1.shape
Out[93]:
(100, 500)
In [94]:
def mul(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for i in range(result.shape[0]):
for j in range(result.shape[1]):
for k in range(mat1.shape[1]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
In [95]:
%timeit mul(arr1, arr2)
10.9 s ± 165 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [96]:
def mul2(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for j in range(result.shape[1]):
for i in range(result.shape[0]):
for k in range(mat1.shape[1]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
In [97]:
%timeit mul2(arr1, arr2)
10.9 s ± 131 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [98]:
def mul3(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for k in range(mat1.shape[1]):
for i in range(result.shape[0]):
for j in range(result.shape[1]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
In [99]:
%timeit mul3(arr1, arr2)
10.8 s ± 67.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [100]:
def mul4(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for k in range(mat1.shape[1]):
for j in range(result.shape[1]):
for i in range(result.shape[0]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
In [101]:
%timeit mul4(arr1, arr2)
10.9 s ± 70.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [102]:
@njit
def mul_njit(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for i in range(result.shape[0]):
for j in range(result.shape[1]):
for k in range(mat1.shape[1]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
@njit
def mul4_njit(mat1, mat2):
result = np.zeros((mat1.shape[0], mat2.shape[1]))
for k in range(mat1.shape[1]):
for j in range(result.shape[1]):
for i in range(result.shape[0]):
result[i][j] += mat1[i][k]*mat2[k][j]
return result
In [109]:
%timeit mul(arr1, arr2)
10.9 s ± 188 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [105]:
%timeit arr1 @ arr2
18.2 ms ± 88.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [107]:
%timeit mul_njit(arr1, arr2)
14.5 ms ± 189 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [106]:
%timeit mul4_njit(arr1, arr2)
12.4 ms ± 144 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [108]:
%timeit np.dot(arr1, arr2)
10.6 ms ± 20.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [ ]: