• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Optimization of numpy array iteration

Resolved: Optimization of numpy array iteration

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m trying to optimize the performance of my python program and I think I have identified this piece of code as bottleneck:
Where red_list, green_list and blue_list are numpy arrays with values like this:

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:
which can be consumed with a for loop, for example to write to disk:
The generator itself can be written either with the core computation vectorized in Python or in a Numba-friendly way. The key is to replace the relatively slow string interpolation with a int-to-hex custom-made computation.
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:
to be used like:

Testing out the output


Some dummy input can be produced easily (r_arr is essentially red_list, etc.):
and tested by consuming the generator to produce a list:
eventually passing through some grouping:

Benchmarks


To give you some ideas of the numbers we could be talking, let us use %timeit on much larger inputs:
To simulate disk writing we could use the following consumer:
where disk writing is simulated with a time.sleep() call with a timeout depending on the logarithm of the object size:
Ignoring the disk-writing simulation, the NumPy-vectorized approach is ~4x faster with the test input sizes, while Numba-accelerated approach gets ~10x to ~100x faster depending on whether the potentially useless conversion to 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!

loops numpy optimization python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Resolved: In a Pinescript v5 Strategy why is my bool not working?

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.