Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
qt stl boost string split performance comparation
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <random>
#include <ctime>
#include <chrono>
#include <cassert>
#include <QString>
#include <QStringList>
#include <boost/algorithm/string.hpp>
int boost_string_tokenizer(const char *raw_str){
std::string str(raw_str);
typedef std::vector<std::string > split_vector_type;
split_vector_type SplitVec; //
boost::split( SplitVec, str, boost::is_any_of(","), boost::token_compress_off );
return SplitVec.size();
}
int stl_string_tokenizer(const char *raw_str){
std::string str(raw_str);
std::istringstream split(str);
std::list<std::string> tokens;
for (std::string each; std::getline(split, each, ',');){
tokens.push_back(std::move(each));
}
return tokens.size();
}
int qt_string_tokenizer(const char* raw_str){
QString str(raw_str);
QStringList strList = str.split(',');
return strList.size();
}
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
using ns = std::chrono::nanoseconds;
using ms = std::chrono::milliseconds;
void benchmark_string_tokenizer(int number_of_test_cases, int max_number_of_words){
std::vector<std::string> string_poll;
std::vector<int> string_number_of_works;
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(1, max_number_of_words); // words for each sentence
std::uniform_int_distribution<> chars(0, 15); // char in base64 chars
std::uniform_int_distribution<> seg_len_gen(1, 100); // words for each sentence
std::cout << "start generate test data..." << std::endl;
for(int i = 0; i < number_of_test_cases; i++){
int number_of_words = dis(gen);
std::cout << "gen data cases " << i << " with number of words " << number_of_words << std::endl;
std::stringstream ss;
for(int j = 0; j < number_of_words; j++){
int segment_length = seg_len_gen(gen);
for(int k = 0; k < segment_length; k++){
ss << base64_chars[chars(gen)];
}
if(j < number_of_words -1){
ss << ',';
}
}
std::string tmp_str = ss.str();
string_poll.push_back(tmp_str);
string_number_of_works.push_back(number_of_words);
}
std::cout << "start testing stl library:" << std::endl;
auto startcputime = std::chrono::high_resolution_clock::now();
for(int i = 0 ; i < number_of_test_cases; i++){
assert(string_number_of_works[i] == stl_string_tokenizer(string_poll[i].c_str()));
}
auto endcputime = std::chrono::high_resolution_clock::now();
std::cout<<"Elapsed time is : "<< std::chrono::duration_cast<ms>(endcputime - startcputime).count()<<" ms"<<std::endl;
std::cout << "start testing qt library:" << std::endl;
startcputime = std::chrono::high_resolution_clock::now();
for(int i = 0 ; i < number_of_test_cases; i++){
assert(string_number_of_works[i] == qt_string_tokenizer(string_poll[i].c_str()));
}
endcputime = std::chrono::high_resolution_clock::now();
std::cout<<"Elapsed time is : "<< std::chrono::duration_cast<ms>(endcputime - startcputime).count()<<" ms"<<std::endl;
std::cout << "start testing boost library:" << std::endl;
startcputime = std::chrono::high_resolution_clock::now();
for(int i = 0 ; i < number_of_test_cases; i++){
assert(string_number_of_works[i] == boost_string_tokenizer(string_poll[i].c_str()));
}
endcputime = std::chrono::high_resolution_clock::now();
std::cout<<"Elapsed time is : "<< std::chrono::duration_cast<ms>(endcputime - startcputime).count()<<" ms"<<std::endl;
}
int main() {
std::cout << stl_string_tokenizer("hello,world,there") << std::endl;
std::cout << qt_string_tokenizer("hello,world,there") << std::endl;
std::cout << boost_string_tokenizer("hello,world,there") << std::endl;
benchmark_string_tokenizer(1000, 10000);
return 0;
}
@WANG-lp

This comment has been minimized.

Copy link
Owner Author

@WANG-lp WANG-lp commented Apr 15, 2018

using following CMakeLists.txt file to compile above code:

mkdir build && cd build
cmake ..
make
cmake_minimum_required(VERSION 3.8)
project(c_plus_plus_stl_qt)

set(CMAKE_CXX_STANDARD 17)

find_package(Boost 1.66 REQUIRED)

find_package(Qt5 COMPONENTS Core REQUIRED)

add_executable(cpp_stl_qt_split qt_stl_boost_split.cpp )

target_link_libraries(cpp_stl_qt_split Qt5::Core ${Boost_LIBRARIES})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment