About a decade ago I was working on an application that used strings quite a lot – enough to be a major factor in performance. At the time I came up with a simple, lightweight C++ string class that took full advantage of the most optimal code – at the time – and benchmarked as radically faster than either the Microsoft or standard C++ string classes. I have re-used the same string class (which is simple enough to write from memory) in just about every C++ application since.
A while back I documented a series of exercises to check whether the string class code was still optimal on current compilers and CPUs. Quite a lot has changed in ten-odd years – so possibly what was once optimal might not be now. Some interesting observations came out of the exercise:
- The specialized x86 instructions for string operations are now slower than performing the same operation with generic x86 instructions.
- Out-of-line calls to the library string functions generally equals or beats simple inlined code.
- The default optimization settings for Microsoft C++ compiler enable the use of x86 string ops – which is not optimal for current generation CPUs.
- My simple string class does indeed still radically outperform the standard C++ string class.
The aim of the exercise was to check my assumptions against current generation compilers and hardware. The end goal was to determine the best performing string class for use in my applications. The upshot was I added a single #pragma to the string class implementation, and performance is once again optimal for current generation hardware.
I do indeed have a better string class – by measured performance.
For some reason I am still getting a trickle of hits from this article, where the article – and the comments – are just plain wrong. Kind of annoying, this.