Question:
I’m trying to optimize the performance of my python program and I think I have identified this piece of code as bottleneck:red_list = [[1, 2, 3, 4, 5], [51, 52, 53, 54, 55]]
green_list = [[6, 7, 8, 9, 10], [56, 57, 58, 59, 60]]
blue_list = [[11, 12, 13, 14, 15], [61, 62, 63, 64, 65]]
At the end of each execution of the inner-for rgb_list is containing the hex values:
rgb_list = [‘01060b’, ‘02070c’, ‘03080d’, ‘04090e’, ‘050a01’]
Now, it is not clear to me how to exploit the potential of numpy arrays but I think there is a way to optimize those two nested loops. Any suggestions?
Answer:
I assume the essential traits of your code could be summarized in the following generator:for
loop, for example to write to disk:This results in substantial speed-up, especially as the size of the input grows (and particularly the second dimension).
Below is the NumPy-vectorized version:
Testing out the output
Some dummy input can be produced easily (
r_arr
is essentially red_list
, etc.):list
:Benchmarks
To give you some ideas of the numbers we could be talking, let us use
%timeit
on much larger inputs:time.sleep()
call with a timeout depending on the logarithm of the object size:list()
with numpy.ndarray.tolist()
is present or not.When it comes to the simulated disk-writing, the faster versions are all more or less equivalent, and noticeably less effective without grouping, resulting in ~2x speed-up. With grouping alone the speed-up gets to be ~2x, but when combining it with the faster approaches, the speed-ups fare between ~3x of the NumPy-vectorized version and the ~7x or ~13x of the Numba-accelerated approaches (with or without
numpy.ndarray.tolist()
).Again, this is with the given input, and under the test conditions. The actual mileage may vary.
If you have better answer, please add a comment about this, thank you!