The best splitter for String in C++

yc
yc
Sep 7, 2018 · 2 min read

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.

Fig 1. The comparison of the time consumptions

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade