http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58358
--- Comment #19 from Chris Jefferson <chris at bubblescope dot net> --- (In reply to Mitsuru Kariya from comment #15) > Created attachment 30775 [details] > Patch > > For your convenience, I attached a patch for this problem. > > This algorithm is always scanned to reverse order. > If a scan fails in less than enough, a re-scan is performed from the point > that advanced necessary elements from the original starting point. > > For example, if __count is 20 and a scan fails after 18 elements succeeded, > a re-scan is performed from the point that advanced 2 elements. This patch is good, but takes a different route., trying to always skip as far forwards as possible. On a small test (compiling the 'search_n/iterator.cc' test, disabling forward checking and bidirection tests, increasing TEST_DEPTH to 23, compiling -O3): Both implementations pass correctness and number of comparison tests. Mitsuru's code is about 1K smaller. My code is slightly faster (3.74sec vs 4.12sec) I think I might choose Mitsuru's code, as his code is smaller in terms of both source and binary, and the performance difference is not that big, but either would be fine.