Ai
1 Star 0 Fork 0

馬迪/string-benchmark

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.cpp 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
馬迪 提交于 2021-10-10 23:09 +08:00 . new: string to number benchmark
#include <benchmark/benchmark.h>
#include <absl/strings/str_split.h>
#include <absl/strings/numbers.h>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/algorithm/string.hpp>
#include <Corrade/Utility/String.h>
#include <array>
#include <charconv>
const std::string S_PI("3.141592657");
// string to number
void s2num_abseil() {
double pi;
absl::SimpleAtod(S_PI, &pi);
}
void s2num_fromchar() {
double pi;
std::from_chars(S_PI.data(), S_PI.data() + S_PI.size(), pi);
}
std::vector<std::string> split_move(const std::string& string, const char delimiter) {
std::vector<std::string> parts;
std::size_t oldpos = 0, pos = std::string::npos;
while((pos = string.find(delimiter, oldpos)) != std::string::npos) {
parts.emplace_back((string.substr(oldpos, pos - oldpos)));
oldpos = pos + 1;
}
if(!string.empty())
parts.emplace_back((string.substr(oldpos)));
return parts;
}
void split_absl() {
std::string text("asfasdfas asf asdf s asdf asdf asdf fdfd fda");
std::vector<std::string> substrs = absl::StrSplit(text, ' ');
}
void split_boost() {
std::string text("asfasdfas asf asdf s asdf asdf asdf fdfd fda");
std::vector<std::string> substrs;
boost::split(substrs, text, boost::is_any_of(" "));
}
void split_corrade() {
std::string text("asfasdfas asf asdf s asdf asdf asdf fdfd fda");
std::vector<std::string> substrs = Corrade::Utility::String::splitWithoutEmptyParts(text, ' ');
}
void split_corrade_move() {
std::string text("asfasdfas asf asdf s asdf asdf asdf fdfd fda");
std::vector<std::string> substrs = split_move(text, ' ');
}
static void BM_absl(benchmark::State& state) {
for (auto _ : state) {
split_absl();
}
}
static void BM_boost(benchmark::State& state) {
for(auto _ : state) {
split_boost();
}
}
static void BM_corrade(benchmark::State& state) {
for(auto _ : state) {
split_corrade();
}
}
static void BM_corrade_move(benchmark::State& state) {
for(auto _ : state) {
split_corrade_move();
}
}
//
static void BM_s2m_abseil(benchmark::State& state) {
for(auto _ : state) {
s2num_abseil();
}
}
static void BM_s2m_fromchar(benchmark::State& state) {
for(auto _ : state) {
s2num_fromchar();
}
}
BENCHMARK(BM_absl);
BENCHMARK(BM_boost);
BENCHMARK(BM_corrade);
BENCHMARK(BM_corrade_move);
BENCHMARK(BM_s2m_abseil);
BENCHMARK(BM_s2m_fromchar);
BENCHMARK_MAIN();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linuxaged/string-benchmark.git
git@gitee.com:linuxaged/string-benchmark.git
linuxaged
string-benchmark
string-benchmark
master

搜索帮助