I tried to improve the performance of the func function, and I found that a simple change in how the aX list is created improves the performance quite a bit:
import timeit import numpy as np def func(a, b): return [_ for _ in a if _ not in b] Na, Nb = 10000, 5000 b = list(np.random.randint(1000, size=Nb))
In Python 2.7.13, this results in:
('Time ab1', 5.296088933944702) ('Time ab2', 1.5520200729370117) ('Time ab3', 1.5581469535827637) ('Ratio 1/2:', 3.412384302428827) ('Ratio 1/3:', 3.3989662667998095)
In Python 3.5.2, the difference is even greater:
Time ab1 6.758207322000089 Time ab2 1.5693355060011527 Time ab3 1.5148192759988888 Ratio 1/2: 4.306413317073784 Ratio 1/3: 4.461395117608107
I need to handle ordered integers (i.e.: a1 or a3 ), so my question is:
Why is a random list processed much faster than a non- ordered list generated with numpy ?
performance python
Gabriel
source share