Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numba import jit as njit
- import numpy as np
- from time import time
- import threading
- import cv2
- @njit('float32(float32[:], float32[:])', nogil=True, nopython=True)
- def f(l, r):
- sums = l * r
- return sum(sums)
- @njit(nogil=True, nopython=True)
- def noop():
- pass
- def f2(l, r):
- sums = l * r
- return sum(sums)
- if __name__ == '__main__':
- # create numpy arr1 len 1000 of random float32
- arr1 = np.random.rand(10_000_000).astype(np.float32)
- # create numpy arr2 len 1000 of random float32
- arr2 = np.random.rand(10_000_000).astype(np.float32)
- # create numpy boolean matrix of 100x100 with random values
- arr3 = np.random.rand(100, 100).astype(np.float32)
- # call njit function
- def runt():
- for i in range(1000):
- noop()
- t = time()
- res = f(arr1, arr2)
- print("numba", threading.current_thread().name, time() - t, res)
- def runt2():
- for i in range(1000):
- noop()
- t = time()
- res = f2(arr1, arr2)
- print("numpy", threading.current_thread().name, time() - t, res)
- def cv2_resize():
- while True:
- noop()
- t = time()
- v = cv2.resize(arr3, (100, 100), cv2.INTER_LINEAR)
- # print("resize", threading.current_thread().name, time() - t)
- t1 = threading.Thread(target=runt2)
- t2 = threading.Thread(target=runt2)
- t3 = threading.Thread(target=runt2)
- t1.start()
- t2.start()
- t3.start()
- t1.join()
- t2.join()
- t3.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement