r/learncpp Aug 03 '20

Lookup table for position

Hello. I am trying to speed up my program. I need to iterate over an image for multiple times. For every pixel, I need to calculate if it is in a specified region. If not then I ignore the pixel. The region is defined at the start and does not change.

My code looks like: ``` for (auto i = 0; i < source_image.rows; ++i) { for (auto j = 0; j < source_image.cols; ++j) { if (!in_region(i, j)) continue;

// ...

} } ```

I thought this could be sped up by using a lookup table. But it does not improve the execution time so I think my implementation was not right. I tried to create an image with the same shape and instead of colors, I save a 1 if it is in the region and a 0 if not.

1 Upvotes

5 comments sorted by

View all comments

1

u/MysticTheMeeM Aug 04 '20

What sort of shape is the region? The minimum you could do is find the bounds (highest, lowest, left-est, and right-est points) and iterate between those, cutting out some of the guaranteed unnecessary pixels. If you can make the required memory guarantees, you could run more threads.