-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumba_ufunc.py
44 lines (31 loc) · 1014 Bytes
/
numba_ufunc.py
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
35
36
37
38
39
40
41
42
43
44
import math
import numpy as np
import time
import numba
# a simple version without using numba
def f(x, y):
return math.pow(x, 3.0) + 4*math.sin(y)
# using numba CPU
@numba.vectorize([numba.float64(numba.float64, numba.float64)], target='cpu')
def f_numba_cpu(x, y):
return math.pow(x,3.0) + 4*math.sin(y)
@numba.vectorize([numba.float64(numba.float64, numba.float64)], target='cuda')
def f_numba_gpu(x,y):
return math.pow(x,3.0) + 4*math.sin(y)
x = np.random.rand(10000000)
res = np.random.rand(10000000)
## Evaluate performance
start = time.time()
for i in range(10000000):
res[i]=f(x[i], x[i])
end = time.time()
print(f"\nElapsed time (Without using Numba) = {end - start} s.")
start = time.time()
res = f_numba_cpu(x, x)
end = time.time()
print(f"Elapsed time (Numba for CPU) = {end - start} s.")
# %%timeit -r 1
start = time.time()
res = f_numba_gpu(x, x)
end = time.time()
print(f"Elapsed time (Numba for GPU) = {end - start} s.\n")