Java.
I have a very small int array (4 elements).
What’s the fastest way to get max value? All values will be between 0 and 255.
Also int array is faster than byte array? Is casting much of a hit?
What’s the fastest way to get sum of all values?
The reason I ask is cause this is a major bottleneck in a program I inherited. They aren’t slow per say, but the guy is calling these methods many many times and it adds up. Eventually I need to rewrite the entire thing, but looking for some quick tricks to speed this up in the short term.
Here’s my go at it… basically an unrolled loop. The JIT would probably have recognized that a loop would have gone from 0 to 3 and unrolled it itself anyway, but who knows…
Sum would obviously just be
a[0] + a[1] + a[2] + a[3].You could also try packing the four 0-255 values in an int, and do some bit-operations instead.