Cool... Guess I should've benchmarked my suggestion instead of just assuming it would be slower than the hash-based ideas.
Actually, unless I've severely mis-benchmarked it, it appears that my modest optimization (combined with blokhead's suggestion of using chop) even outdoes BrowserUK's bitwise version and still more so with fewer lines (based on BrowserUK's test code, I'm comparing every pair of lines):
100 lines/100 iterations:
Rate bitwise ifchop
bitwise 33.1/s -- -41%
ifchop 56.5/s 71% --
10 lines/10000 iterations:
Rate bitwise ifchop
bitwise 3226/s -- -57%
ifchop 7463/s 131% --
1000 lines/5 iterations:
s/iter bitwise ifchop
bitwise 3.06 -- -23%
ifchop 2.37 29% --
(I actually suspect I'm doing something wrong in my test because I don't see why the number of lines would affect the relative performance at all. Update: I was doing something wrong. I forgot to pull the strings out of the array and was just comparing indexes in my version. With my version fixed to actually do the comparisons, the bitwise version is much faster, as I had initially expected:
Rate ifchop bitwise
ifchop 4.22/s -- -87%
bitwise 33.3/s 690% --
)
Benchmark code:
|