Created
March 6, 2015 22:22
-
-
Save refi64/e69b605a287cedcd57f5 to your computer and use it in GitHub Desktop.
C++ string implode benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a simple benchmark of the C++ string implode algorithms as stated at | |
http://stackoverflow.com/questions/5689003/ | |
how-to-implode-a-vector-of-strings-into-a-string-the-elegant-way. | |
This uses Celero. | |
Results on my machine, compiling with Clang SVN and -O3: | |
[==========] | |
[ CELERO ] | |
[==========] | |
[ STATUS ] Timer resolution: 1000000.000000 us | |
[==========] | |
[ STAGE ] Baselining | |
[==========] | |
[ RUN ] Base [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.Base 0.922866 sec. [4.525000e-07 us/call] [220994.475138 calls/sec] | |
[ BASELINE ] Joins.Base 1.000000 [SD: 53.759298, V: 2890.062106, K: 137.300267] | |
[==========] | |
[ STAGE ] Benchmarking | |
[==========] | |
[ RUN ] BaseWithGenericReserve [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.BaseWithGenericReserve 0.408367 sec. [2.025000e-07 us/call] [493827.160494 calls/sec] | |
[ BASELINE ] Joins.BaseWithGenericReserve 0.447514 [SD: 9.637073, V: 92.873184, K: 278.078086] | |
[ RUN ] BaseWithSlowReserve [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.BaseWithSlowReserve 0.945489 sec. [4.690000e-07 us/call] [213219.616205 calls/sec] | |
[ BASELINE ] Joins.BaseWithSlowReserve 1.036464 [SD: 11.945148, V: 142.686566, K: 783.934323] | |
[ RUN ] StringStream [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.StringStream 1.879999 sec. [9.370000e-07 us/call] [106723.585912 calls/sec] | |
[ BASELINE ] Joins.StringStream 2.070718 [SD: 7.909966, V: 62.567567, K: 32.086897] | |
[ RUN ] Accum [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.Accum 1.700649 sec. [8.435000e-07 us/call] [118553.645525 calls/sec] | |
[ BASELINE ] Joins.Accum 1.864088 [SD: 37.465898, V: 1403.693492, K: 122.419338] | |
[ RUN ] CopySStream [1000 samples of 2000 calls each.] | |
[ DONE ] Joins.CopySStream 2.143611 sec. [1.067500e-06 us/call] [93676.814988 calls/sec] | |
[ BASELINE ] Joins.CopySStream 2.359116 [SD: 27.531758, V: 757.997677, K: 608.496731] | |
[==========] | |
[ STAGE ] | |
[==========] | |
*/ | |
#include <celero/Celero.h> | |
#include <algorithm> | |
#include <iterator> | |
#include <sstream> | |
#include <numeric> | |
#include <vector> | |
#include <string> | |
#define SAMPLES 1000 | |
#define CALLS 2000 | |
using namespace std; | |
vector<string> vec{"a", "bbasdas", "asd;askoew", "asd0iqpw213odm"}; | |
CELERO_MAIN | |
BASELINE(Joins, Base, SAMPLES, CALLS) { | |
string s; | |
for (auto it = vec.begin(); it != vec.end(); ++it) { | |
s += *it; | |
if (it+1 != vec.end()) s += ","; | |
} | |
celero::DoNotOptimizeAway<string>(forward<string>(s)); | |
} | |
BENCHMARK(Joins, BaseWithGenericReserve, SAMPLES, CALLS) { | |
string s; | |
s.reserve(vec.size()*20); | |
for (auto it = vec.begin(); it != vec.end(); ++it) { | |
s += *it; | |
if (it+1 != vec.end()) s += ","; | |
} | |
celero::DoNotOptimizeAway<string>(forward<string>(s)); | |
} | |
BENCHMARK(Joins, BaseWithSlowReserve, SAMPLES, CALLS) { | |
string s; | |
size_t len{0}; | |
for (string part : vec) len += part.length()+1; | |
--len; | |
for (auto it = vec.begin(); it != vec.end(); ++it) { | |
s += *it; | |
if (it+1 != vec.end()) s += ","; | |
} | |
celero::DoNotOptimizeAway<string>(forward<string>(s)); | |
} | |
BENCHMARK(Joins, StringStream, SAMPLES, CALLS) { | |
ostringstream ss; | |
for (size_t i=0; i<vec.size(); ++i) { | |
if (i) ss << ","; | |
ss << vec[i]; | |
} | |
celero::DoNotOptimizeAway<string>(ss.str()); | |
} | |
BENCHMARK(Joins, Accum, SAMPLES, CALLS) { | |
string s = accumulate(vec.begin(), vec.end(), string(""), | |
[](string l, string r) {return l+r+",";}); | |
celero::DoNotOptimizeAway<string>(s.substr(0, s.length()-1)); | |
} | |
BENCHMARK(Joins, CopySStream, SAMPLES, CALLS) { | |
ostringstream ss; | |
copy(vec.begin(), vec.end(), ostream_iterator<string>(ss, ",")); | |
string s = ss.str(); | |
celero::DoNotOptimizeAway<string>(s.substr(0, s.length()-1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment