Skip to content

Commit

Permalink
update string series
Browse files Browse the repository at this point in the history
  • Loading branch information
firedtoad committed May 8, 2022
1 parent b4c0add commit fd95f21
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*.out
*.app
.idea
/cmake-*
/cmake-build-release
/cmake-build-debug
/cmake-build-debug-coverage
Expand Down
17 changes: 11 additions & 6 deletions src/astar/bench_astar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <thread>
#include <benchmark/benchmark.h>

const int WIDTH = 500;
const int HEIGHT = 500;
const int WIDTH = 499;
const int HEIGHT = 499;
const bool rand_pos = false;
static AStar::Generator generator;
static AStar::Vec2i start_pos = {0, 0};
Expand All @@ -19,7 +19,7 @@ void BenchmarkAstar(benchmark::State &state) {
for (auto _ : state) {
generator.findPath(start_pos, end_pos);
}
state.SetItemsProcessed(state.iterations() );
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BenchmarkAstar);

Expand All @@ -32,7 +32,10 @@ int main(int argc, char **argv) {
std::random_device rd;
std::default_random_engine gen{rd()};
std::uniform_int_distribution<> dis(0, 10000);
std::array<std::array<char, WIDTH>, HEIGHT> cc;
std::vector<std::vector<char>> cc(WIDTH);
for (auto &it : cc) {
it.resize(HEIGHT);
}
generator.SetCollision([&cc](const AStar::Vec2i &cord) {
int x = cord.x;
int y = cord.y;
Expand All @@ -45,7 +48,9 @@ int main(int argc, char **argv) {
generator.setHeuristic(AStar::Heuristic::octagonal);
const int prob = 2000;
auto c_gen = [&cc, &dis, &gen]() {
cc.fill({});
for (auto &it : cc) {
std::fill(it.begin(), it.end(), 0);
}
for (auto &it : cc) {
for (auto &cit : it) {
if (dis(gen) < prob) {
Expand All @@ -60,7 +65,7 @@ int main(int argc, char **argv) {
start_pos = {dis(gen), dis(gen)};
end_pos = {dis(gen), dis(gen)};
}
std::cout << start_pos <<" : "<< end_pos << '\n';
std::cout << start_pos << " : " << end_pos << '\n';
do {
c_gen();

Expand Down
12 changes: 8 additions & 4 deletions src/string/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ add_executable(string_split string_split.cpp)
add_executable(string_from string_from.cpp)
add_executable(string_to string_to.cpp)
add_executable(string_format string_format.cpp)
target_link_libraries(string_split PRIVATE absl_strings)
target_link_libraries(string_from PRIVATE absl_strings absl_str_format_internal)
target_link_libraries(string_to PRIVATE absl_strings)
add_executable(string_cat string_cat.cpp)

target_compile_features(string_format PRIVATE cxx_std_20)
target_link_libraries(string_format PRIVATE absl_strings absl_str_format_internal fmt)

target_link_libraries(string_split PRIVATE absl_strings protobuf)
target_link_libraries(string_from PRIVATE absl_strings absl_str_format_internal protobuf)
target_link_libraries(string_to PRIVATE absl_strings protobuf)
target_link_libraries(string_format PRIVATE absl_strings absl_str_format_internal fmt protobuf)
target_link_libraries(string_cat PRIVATE absl_strings absl_str_format_internal fmt protobuf)
96 changes: 96 additions & 0 deletions src/string/string_cat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

#include <benchmark/benchmark.h>
#include <sstream>
#include <random>
#include <fmt/format.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_format.h>
#include <boost/algorithm/string.hpp>
#include <google/protobuf/stubs/strutil.h>

std::string tag = "1234567890123456";
std::string tag1 = "1234567890123456";

static void BenchStrNCat(benchmark::State &state) {
for (auto _ : state) {
char dest[128]{};
strncat(dest, tag.c_str(), tag.size());
strncat(&dest[tag.size()], ":", 2);
strncat(&dest[tag.size() + 1], tag1.c_str(), tag1.size());
benchmark::DoNotOptimize(dest);
}
}
BENCHMARK(BenchStrNCat);

static void BenchStdStringCat(benchmark::State &state) {
for (auto _ : state) {
auto r = tag;
r += ":";
r += tag1;
r += tag1;
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchStdStringCat);

static void BenchStreamStringCat(benchmark::State &state) {

for (auto _ : state) {
std::stringstream ss;
ss << tag << ":" << tag1 << tag1;
auto r = ss.str();
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchStreamStringCat);

static void BenchBoostStrCat(benchmark::State &state) {

for (auto _ : state) {
auto r = boost::join<std::vector<std::string>>({tag, ":", tag1}, "");
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchBoostStrCat);

static void BenchAbStrCat(benchmark::State &state) {
for (auto _ : state) {
auto r = absl::StrCat(tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchAbStrCat);

static void BenchPBStrCat(benchmark::State &state) {
for (auto _ : state) {
auto r = google::protobuf::StrCat(tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchPBStrCat);

static void BenchAbStrFormat(benchmark::State &state) {
for (auto _ : state) {
auto r = absl::StrFormat("%s%s%s%s", tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchAbStrFormat);

static void BenchFormat(benchmark::State &state) {
for (auto _ : state) {
auto r = fmt::format("{}{}{}{}", tag, ":", tag1, tag1);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK(BenchFormat);

int main(int argc, char **argv) {

tag = std::to_string(std::mt19937_64{std::random_device{}()}());
tag1 = std::to_string(std::mt19937_64{std::random_device{}()}());
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
19 changes: 16 additions & 3 deletions src/string/string_from.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
#include <absl/strings/numbers.h>
#include <absl/strings/str_format.h>
#include <boost/lexical_cast.hpp>
#include <google/protobuf/stubs/strutil.h>

uint64_t tag = 1234567890123456;
float tagf = M_PI;

template<typename S, typename T>
void NumberToString(T &t, std::string &str) {
S oss(str);
thread_local static S oss;
oss.str("");
oss << t;
str = oss.str();
}
Expand Down Expand Up @@ -149,7 +151,7 @@ BENCHMARK_TEMPLATE(BenchAbSixDigitsToBuffer, float);
template<typename T>
static void BenchAbStrFormat(benchmark::State &state) {
for (auto _ : state) {
auto r=absl::StrFormat("%d",tag);
auto r = absl::StrFormat("%d", tag);
benchmark::DoNotOptimize(r);
}
}
Expand All @@ -158,12 +160,23 @@ BENCHMARK_TEMPLATE(BenchAbStrFormat, uint64_t);
template<typename T>
static void BenchAbStrFormatF(benchmark::State &state) {
for (auto _ : state) {
auto r=absl::StrFormat("%f",tagf);
auto r = absl::StrFormat("%f", tagf);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK_TEMPLATE(BenchAbStrFormatF, float);

template<typename T>
static void BenchPBSimpleItoa(benchmark::State &state) {
T r = 0;
for (auto _ : state) {
char buf[64]{};
auto r = google::protobuf::FastUInt64ToBuffer(tag, buf);
benchmark::DoNotOptimize(r);
}
}
BENCHMARK_TEMPLATE(BenchPBSimpleItoa, uint64_t);

int main(int argc, char **argv) {

tag = std::mt19937_64{std::random_device{}()}();
Expand Down
19 changes: 18 additions & 1 deletion src/string/string_split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sstream>
#include <memory>
#include <unordered_map>
#include <google/protobuf/stubs/strutil.h>
#include <absl/strings/str_split.h>
#include <boost/algorithm/string.hpp>

Expand Down Expand Up @@ -87,7 +88,7 @@ static void BenchABSplit(benchmark::State &state) {
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
int r = 0;
for (auto _ : state) {
auto vec = absl::StrSplit(tag, ",");
auto vec = absl::StrSplit(tag, ",");
for (auto &i : vec) {
r += i.size();
}
Expand All @@ -97,6 +98,22 @@ static void BenchABSplit(benchmark::State &state) {

BENCHMARK(BenchABSplit)->ThreadRange(1, 8);

static void BenchPBSplit(benchmark::State &state) {
std::string tag = "1,2,3,4,5,6,7,8,9,0,1,2,3,4,6,1,2,3,4,56,7,8,9,09,12";
int r = 0;
std::vector<std::string> vec;
for (auto _ : state) {
google::protobuf::SplitStringAllowEmpty(tag, ",", &vec);
for (auto &i : vec) {
r += i.size();
}
vec.clear();
}
benchmark::DoNotOptimize(r);
}

BENCHMARK(BenchPBSplit)->ThreadRange(1, 8);

int main(int argc, char **argv) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
Expand Down
33 changes: 33 additions & 0 deletions src/string/string_to.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <cmath>
#include <charconv>
#include <absl/strings/numbers.h>
#include <absl/strings/charconv.h>
#include <boost/lexical_cast.hpp>
#include <google/protobuf/stubs/strutil.h>

std::string tag = "1234567890123456";
std::string tagf = std::to_string(M_PI);
Expand Down Expand Up @@ -86,6 +88,17 @@ static void BenchStdCharConvInt(benchmark::State &state) {

BENCHMARK_TEMPLATE(BenchStdCharConvInt, uint64_t);

template<typename T>
static void BenchABCharConvIntF(benchmark::State &state) {
for (auto _ : state) {
T r{};
absl::from_chars(tagf.data(), tagf.data() + tagf.size(), r);
benchmark::DoNotOptimize(r);
}
}

BENCHMARK_TEMPLATE(BenchABCharConvIntF, float);

template<typename T>
static void BenchBoostStringTo(benchmark::State &state) {
for (auto _ : state) {
Expand Down Expand Up @@ -128,6 +141,26 @@ static void BenchAbSimpleToFloat(benchmark::State &state) {
}
BENCHMARK_TEMPLATE(BenchAbSimpleToFloat, float);

template<typename T>
static void BenchPBStrAppend(benchmark::State &state) {
for (auto _ : state) {
std::string s;
google::protobuf::StrAppend(&s, tag);
benchmark::DoNotOptimize(s);
}
}
BENCHMARK_TEMPLATE(BenchPBStrAppend, uint64_t);

template<typename T>
static void BenchPBStrAppendF(benchmark::State &state) {
for (auto _ : state) {
std::string s;
google::protobuf::StrAppend(&s, tagf);
benchmark::DoNotOptimize(s);
}
}
BENCHMARK_TEMPLATE(BenchPBStrAppendF, float);

int main(int argc, char **argv) {

tag = std::to_string(std::mt19937_64{std::random_device{}()}());
Expand Down

0 comments on commit fd95f21

Please sign in to comment.