Thursday, December 01, 2016
I found this optimization question on the
comp.lang.idl-pvwave, and decided to give it a try. The question was to implement
an algorithm that replaces every element in a 2-D array with its neighborhood
maximum value. In this case the neighborhood size was 101x101 (i.e. -50 to +50),
and the array size was 3200x3248. The nested FOR loop approach looks like the
following code snippet.
My first thought was to use the > operator which returns
the maximum of 2 arguments. It operates on arrays, and in conjunction with the
SHIFT function it serves to return the larger of 2 neighbors. The other trick
here is that since we are looking for a 101x101 neighborhood maximum, we can
use a combination of smaller neighborhood maxima as input in a structured way
in order to achieve the exact 101x101 neighborhood size. The code that I ended
up with after some trial and error was the following.
I didn’t say that optimized code always looks pretty, but
the goal here is to run fast. Adding in some result comparison checking to make
sure the results are equivalent.
Finally, here are the results, which yielded an impressive
amount of speed-up, the execution time went from 189.4 seconds to 1.4 seconds,
and the results are identical:
Number of views (912)/Comments (0)
Base 60 encoding of positive floating point numbers in IDL
First Valley Shadow Detection
Sign Up for News & Updates: Stay informed with the latest news, events, technologies and special offers.