Advertisement
user_137

comparison

Oct 22nd, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import numpy as np, numba as nb
  2. from numpy import array as npa
  3. from numba import jit, njit
  4. import matplotlib.pyplot as plt
  5. import random
  6. import time
  7. import math
  8.  
  9.  
  10. @nb.vectorize('i8(i8,i8)')
  11. def diffIt(h, l):
  12.     return h - l
  13.  
  14.  
  15. @njit('(i8[:, :], i8[:])')
  16. def diff_temps(aa, bb):
  17.     for i in range(len(aa)):
  18.         bb[i] = aa[i, 0] - aa[i, 1]
  19.     return bb
  20.  
  21.  
  22. N = 10000000
  23. highTemps = list(range(10, N+10))
  24. lowTemps = [i - random.randint(0, 20) for i in highTemps]
  25. aTemps = np.vstack([np.array(highTemps), np.array(lowTemps)]).T
  26. print(aTemps.shape)
  27.  
  28. start = time.time()
  29. out = [h - l for (h, l) in zip(highTemps, lowTemps)]
  30. t1 = time.time() - start
  31. start = time.time()
  32. out = aTemps[:, 0] - aTemps[:, 1]
  33. t2 = time.time() - start
  34. #
  35. start = time.time()
  36. out = diffIt(aTemps[:, 0], aTemps[:, 1])
  37. t3 = time.time() - start
  38. start = time.time()
  39. b = np.empty(N, dtype=int)
  40. out = diff_temps(aTemps, b)
  41. t4 = time.time() - start
  42. print('ListComp takes %.5fs, Numpy takes %.5fs' %(t1, t2))
  43. print('NumbaUfunc takes %.5fs, Numba takes %.5fs' %(t3, t4))
  44. print
  45. print(out.shape, out)
  46.  
  47.  
  48.  
  49. # (5000000, 2)
  50. # ListComp takes 1.36459s, Numpy takes 0.01582s
  51. # NumbaUfunc takes 0.01582s, Numba takes 0.01049s
  52. # ((5000000,), array([17,  8,  8, ..., 19, 11, 17]))
  53. # Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement