The best splitter for String in C++
Introduction
String splitter is very important while you handle String. There’re many ways to help you to tackle String including boost library in C++. But what kind of solutions with better performance. I just set a benchmark to figure out which is better for common solutions. I come up with the conclusion first : Strtok and emplace_back(C++11) is the best.
Details
I literally adopted two methods: Strtok and Getline(C++ way). Also, I exploited C++11 features(emplace_back for vector and rvalue reference as well as std::move). Theoretically, emplace_back and move from would bring great benefit for big context. I measure the time interval where the context starts to be split into patterns.
Implementation
First, I implement time interval macro and exploit scope principle to release memory from a stack. For sure, deconstructor will be invoked, the time interval method.
While deconstructor is invoked, TimeInterval will present total execution time. Therefore, we just need to rap our measure scope.
Secondly, let’s take a view of our data
I created a huge context, and construct the vector with constant capacity, and avoid allocating memory dynamically. Since allocating memory will cause some resource and time, we try to avoid this factor.
Let’s take into split implementation.
Consequence
As you see, I adopt four strategies. The last one use c++ way, string stream. Unfortunately, the istringstream wouldn’t bring better performance, even though, I used emplace_back to reduce the resource consumption.
According to Fig 1, we can find out that strtok plus emplace_back indeed has great performance.
Summary
Though, istringstream can handle huge context than std::copy way, it doesn’t bring great performance for handling string pattern. Therefore, I still recommend strtok.