Advertisement
Guest User

Untitled

a guest
Jul 21st, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. from numba import jit as njit
  2. import numpy as np
  3. from time import time
  4. import threading
  5. import cv2
  6.  
  7. @njit('float32(float32[:], float32[:])', nogil=True, nopython=True)
  8. def f(l, r):
  9.     sums = l * r
  10.     return sum(sums)
  11.  
  12.  
  13. @njit(nogil=True, nopython=True)
  14. def noop():
  15.     pass
  16.  
  17.  
  18. def f2(l, r):
  19.     sums = l * r
  20.     return sum(sums)
  21.  
  22. if __name__ == '__main__':
  23.     # create numpy arr1 len 1000 of random float32
  24.     arr1 = np.random.rand(10_000_000).astype(np.float32)
  25.     # create numpy arr2 len 1000 of random float32
  26.     arr2 = np.random.rand(10_000_000).astype(np.float32)
  27.     # create numpy boolean matrix of 100x100 with random values
  28.     arr3 = np.random.rand(100, 100).astype(np.float32)
  29.  
  30.     # call njit function
  31.     def runt():
  32.         for i in range(1000):
  33.             noop()
  34.             t = time()
  35.             res = f(arr1, arr2)
  36.             print("numba", threading.current_thread().name, time() - t, res)
  37.  
  38.     def runt2():
  39.         for i in range(1000):
  40.             noop()
  41.             t = time()
  42.             res = f2(arr1, arr2)
  43.             print("numpy", threading.current_thread().name, time() - t, res)
  44.  
  45.     def cv2_resize():
  46.         while True:
  47.             noop()
  48.             t = time()
  49.             v = cv2.resize(arr3, (100, 100), cv2.INTER_LINEAR)
  50.             # print("resize", threading.current_thread().name, time() - t)
  51.  
  52.     t1 = threading.Thread(target=runt2)
  53.     t2 = threading.Thread(target=runt2)
  54.     t3 = threading.Thread(target=runt2)
  55.  
  56.     t1.start()
  57.     t2.start()
  58.     t3.start()
  59.  
  60.     t1.join()
  61.     t2.join()
  62.     t3.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement