What is the most efficient method to find x contiguous values of y in an array?
algorithm, c, c++, performance, profiling
Solution
You tagged c++ so I assume you have STL algorithms available:
std::search_n(buffer, bufferEnd, desiredLength, 0xffffffff);
Problem
Running my app through callgrind revealed that this line dwarfed everything else by a factor of about 10,000. I'm probably going to redesign around it, but it got me wondering; Is there a better way to do it? Here's what I'm doing at the moment: ``` int i = 1; while ( ( (*(buffer++) == 0xffffffff && ++i) || (i = 1) ) && i < desiredLength + 1 && buffer < bufferEnd ); ``` It's looking for the offset of the first chunk of desiredLength 0xffffffff values in a 32 bit unsigned int array. It's significantly faster than any implementations I could come up with involving an inner loop. But it's still too damn slow.