Created
July 3, 2021 11:35
-
-
Save emadflash/ea43fbc88bb159e64e7255f89077b1ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
template <typename T, typename It> auto split(It begin, It end, char delm) { | |
It cursor = begin; | |
It token; | |
T sv_line; | |
std::vector<T> v_sv; | |
while (cursor != end) { | |
token = std::find(cursor, end, delm); | |
if (token != end) { | |
sv_line = std::string(cursor, token); | |
v_sv.push_back(sv_line); | |
cursor = std::next(token); | |
} else { | |
v_sv.push_back(std::string(cursor, end)); | |
break; | |
} | |
} | |
return v_sv; | |
} | |
template <typename T, typename It> auto split2(It begin, It end, char delm) { | |
It cursor = begin; | |
It token; | |
T sv_line; | |
std::vector<T> v_sv; | |
while (cursor != end) { | |
token = std::find(cursor, end, delm); | |
if (token == end) break; | |
sv_line = std::string(cursor, token); | |
v_sv.push_back(sv_line); | |
cursor = std::next(token); | |
} | |
v_sv.push_back(T(cursor, end)); | |
return v_sv; | |
} | |
template <typename It> auto get_iters_for_split(It begin, It end, char delm) { | |
std::vector<It> iters; | |
iters.push_back(begin); | |
for(auto it=begin; it != end; ++it) { | |
if (*it == delm) { | |
iters.push_back(std::next(it)); | |
} | |
} | |
iters.push_back(end); | |
return iters; | |
} | |
template <typename T, typename It> auto split3(It begin, It end, char delm) { | |
std::vector<std::string::iterator> _tmp_iter; | |
_tmp_iter.shrink_to_fit(); | |
std::vector<std::string> strings_vec; | |
auto iters = get_iters_for_split(begin, end, delm); | |
for(auto& m : iters) { | |
if (_tmp_iter.empty()) { | |
_tmp_iter.push_back(m); | |
} else { | |
auto xm = _tmp_iter.back(); | |
_tmp_iter.pop_back(); | |
_tmp_iter.push_back(m); | |
strings_vec.push_back(std::string(xm, m)); | |
} | |
} | |
return strings_vec; | |
} | |
static void SplitWithIf(benchmark::State& state) { | |
// Code inside this loop is measured repeatedly | |
std::string s { "this is a string" }; | |
for (auto _ : state) { | |
auto split__ = split<std::string>(s.begin(), s.end(), ' '); | |
// Make sure the variable is not optimized away by compiler | |
benchmark::DoNotOptimize(split__); | |
} | |
} | |
// Register the function as a benchmark | |
BENCHMARK(SplitWithIf); | |
static void SplitWithoutIf(benchmark::State& state) { | |
// Code before the loop is not measured | |
std::string s { "this is a string" }; | |
for (auto _ : state) { | |
auto split__ = split2<std::string>(s.begin(), s.end(), ' '); | |
// Make sure the variable is not optimized away by compiler | |
benchmark::DoNotOptimize(split__); | |
} | |
} | |
BENCHMARK(SplitWithoutIf); | |
static void SplitWithIters(benchmark::State& state) { | |
// Code before the loop is not measured | |
std::string s { "this is a string" }; | |
for (auto _ : state) { | |
auto split__ = split3<std::string>(s.begin(), s.end(), ' '); | |
// Make sure the variable is not optimized away by compiler | |
benchmark::DoNotOptimize(split__); | |
} | |
} | |
BENCHMARK(SplitWithIters); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment