1

I need to product numpy arrays but it's too slow for me. How to fasten it?

import time
import numpy as np
N = 2e6
a = np.arange(N)
b = np.arange(N)
c = np.arange(N)
d = np.arange(N)
t1 = time.time()
e = a*b*c*d
t1 = time.time() - t1
print(t1)

I tried to product lists but I don't know how to do it without loops

list_a = np.array(a).tolist()
list_b = np.array(b).tolist()
list_c = np.array(c).tolist()
list_d = np.array(d).tolist()
list_e = list_a * list_b * list_b * list_d # Error
bluesky
  • 243
  • 2
  • 12
  • 2
    Using lists will *definitely* not make things faster. I would expect it to make things much, much slower. – Karl Knechtel Sep 17 '20 at 01:45
  • 2
    Anyway, this takes around 21 milliseconds on my machine. Just how fast do you need it to be? You are requesting 6 million multiplication floating-point operations - plus all the boiler-plate of moving the numbers around - and home computers operate in the single-digit GHz range... do the math. – Karl Knechtel Sep 17 '20 at 01:50
  • I tested the straightforward approach with lists. It takes about 12 times as long. – Karl Knechtel Sep 17 '20 at 01:54
  • 1
    Elementwise multiplication is not defined for lists. `*` is use for list replication. – hpaulj Sep 17 '20 at 02:37

1 Answers1

2

numpy does it very fast, much faster than using fors or other ways of Python looping but you can definitely win even more speed using numexpr:

import numpy as np
import numexpr as ne
N = 2e6
a = np.arange(N)
b = np.arange(N)
c = np.arange(N)
d = np.arange(N)

print(np.array_equal(a*b*c*d, ne.evaluate('a*b*c*d')))
#prints True

Here are the timings:

%timeit a*b*c*d
%timeit ne.evaluate('a*b*c*d') 
30.7 ms ± 480 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
10 ms ± 101 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
mathfux
  • 5,759
  • 1
  • 14
  • 34