The Revised String Iteration Benchmark

In this post I’m going to discuss again the string benchmark I did before to find out what is the fastest way to iterate over an std::string. If you haven’t read the previous post on this subject go a head and read it as it covers the basic idea behind this benchmark. As I did the last time I did the benchmark, I check 5 ways of iteration:

  • Iterating using the native indexes.
  • Iterating using the string::at method.
  • Iterating using indexes over the C string representation of the string.
  • Iterating using pointers over the C string representation of the string.
  • Iterating using the STL iterators.

The basic operation that tests the performance of each kind of iteration hasn’t been changed, and as in the last benchmark I used a test string of 50,000,000 characters. Instead I’ve altered the timing mechanism. In the first test, each one of the iteration methods had it’s one executable and the performance was timed as the execution time timed by the time command for each executable, this methods has a problematic overhead in the form of the time it takes to load to memory each one of the 50MB binaries. In this test I’ve fixed this problem and now the tests timed using clock_gettime. This allowed me to preform all tests from within the same executable and without the overhead of the actual loading time. As a bonus the timer in clock_gettime has much higher resolution, so the results should be much more accurate.

To preform the test download the benchmark’s source code – stringbenchmark2.tar.gz. After you download it go to the directory where you saved the archive and execute

tar -zxvf stringbenchmark2.tar.gz
cd stringbenchmark2
make
./benchmark

The last command is the actual benchmark. It will ran each of the tests and print its timing in the format of seconds:nanoseconds. For example a typical output of the benchmark on my system looks like this:

String iteration benchmark
String length: 50000000
iteration using indexes
0:588484755

iteration using indexes over C string
0:617691596

iteration using string::at()
0:615612772

iteration using pointers over C string
0:515342342

iteration using iterators
2:166078723

As you can see the results (in the relative speeds between the iteration methods) remained the same as in the last benchmark. Iterating using pointers over the C string representation is the fastest method by a small margin and using iterators is the slowest by far. This time the results are much more accurate and the difference between closely performing methods such as string::at and using index over the C string repesentation is easily noticeable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.