盒子
盒子

Test of boost string performance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
#include <boost/algorithm/string.hpp>
#include <vector>
using std::string;
using std::vector;
void
split(const string& input, const string& delim, vector<string>& tokens)
{
if (!delim.empty())
{
tokens.clear();
size_t start = 0;
size_t end = 0;
while (end != string::npos)
{
end = input.find(delim, start);
if (end >= start)
{
tokens.push_back(input.substr(start, end - start));
}
// Advance start past the delim.
start = end + delim.size();
}
}
}
int main()
{
auto start = std::chrono::system_clock::now();
for (int i = 0; i < 10000; i++)
{
string tests = "a,v,v,v,x,sd,asf,e,r,afds,g";
vector<string> ret;
split(tests, ",", ret);
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "basicstring elapsed time: " << elapsed_seconds.count() << "s\n";
start = std::chrono::system_clock::now();
for (int i = 0; i < 10000; i++)
{
string tests = "a,v,v,v,x,sd,asf,e,r,afds,g";
vector<string> ret;
boost::split(ret, tests, boost::is_any_of(","));
}
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
std::cout << "boost:split elapsed time: " << elapsed_seconds.count() << "s\n";
return 0;
}

呼呼呼山(http://code4fun.me)
2019-03-18 17:30:43