4 ms·
Putting benchmarks where my mouth is: https://github.com/tylerhou/benchmarks/blob/main/vowels-benchmark_test.cc https://github.com/tylerhou/benchmarks/blob/main
by tylerhou 1y ago
Putting benchmarks where my mouth is: https://github.com/tylerhou/benchmarks/blob/main/vowels-benchmark_test.cc https://github.com/tylerhou/benchmarks/blob/main/vowels-benc...
Writing implementations in C++, using a reasonable encoding of how an efficient regex compiler might compile the vowel regex, the regex implementation outperforms the loop-based implementations significantly in all cases except for long strings with vowels on M1 Max MBP.
Strings were generated randomly following the regex [0-9A-Za-z]. Short strings were between 5-20 chars. Long strings were 5000 chars.
The speedups are:
- Short strings & with vowels: regex is 2.6x faster
- Short strings & no vowels: regex is 5.9x faster
- Long strings & with vowels: loop is 329x faster. This is expected, as because the regex implementation is branchless, it must always search through the whole string.
- Long strings & no vowels: regex is 1.5x faster.
If you add an early return to the regex implementation, the regex with early return becomes strictly faster than the loop versions. The early return is slower than the no early return for the short strings & no vowels case because the extra branch has a cost. Full outputs in a comment at the bottom of the linked file.
- SleepyMyroslav 1y agoWhy do you loop over haystack multiple times though? If you iterate over long string once and write fixed loop with vowels checks in way that will be friendly to autovectorize optimization it might be faster and more idiomatic C or C++.
- tylerhou 1y agoThanks for reminding me, I meant to also benchmark the interchanged version. I updated the file above with the new benchmark results. The original commenter called `find()` once per vowel, so that's why I benchmarked the regex against the less-idiomatic code. The interchanged version (loop over haystack outside, over vowels inside) is (mildly) faster than all the regex versions except for short strings & no vowels. One thing to note is that all the loop versions are not easily generalizable to non-ASCII strings, while the regex version is fairly easily.
- SleepyMyroslav 1y agoI poked that loop in compiler explorer for few minutes and I think its allergic to autovectorization. Lets assume that properly vectorized regexp from proper language in the future wins :)