Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np, numba as nb
- from numpy import array as npa
- from numba import jit, njit
- import matplotlib.pyplot as plt
- import random
- import time
- import math
- @nb.vectorize('i8(i8,i8)')
- def diffIt(h, l):
- return h - l
- @njit('(i8[:, :], i8[:])')
- def diff_temps(aa, bb):
- for i in range(len(aa)):
- bb[i] = aa[i, 0] - aa[i, 1]
- return bb
- N = 10000000
- highTemps = list(range(10, N+10))
- lowTemps = [i - random.randint(0, 20) for i in highTemps]
- aTemps = np.vstack([np.array(highTemps), np.array(lowTemps)]).T
- print(aTemps.shape)
- start = time.time()
- out = [h - l for (h, l) in zip(highTemps, lowTemps)]
- t1 = time.time() - start
- start = time.time()
- out = aTemps[:, 0] - aTemps[:, 1]
- t2 = time.time() - start
- #
- start = time.time()
- out = diffIt(aTemps[:, 0], aTemps[:, 1])
- t3 = time.time() - start
- start = time.time()
- b = np.empty(N, dtype=int)
- out = diff_temps(aTemps, b)
- t4 = time.time() - start
- print('ListComp takes %.5fs, Numpy takes %.5fs' %(t1, t2))
- print('NumbaUfunc takes %.5fs, Numba takes %.5fs' %(t3, t4))
- print
- print(out.shape, out)
- # (5000000, 2)
- # ListComp takes 1.36459s, Numpy takes 0.01582s
- # NumbaUfunc takes 0.01582s, Numba takes 0.01049s
- # ((5000000,), array([17, 8, 8, ..., 19, 11, 17]))
- # Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement