Skip to content

Commit

Permalink
Added StringZilla as 3rd party benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
WillisMedwell committed Feb 6, 2024
1 parent e2a2fac commit 044ce27
Show file tree
Hide file tree
Showing 5 changed files with 1,366 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ if(BUILD_UTILY_BENCHMARKS)
target_compile_options(UtilyBenchmark PRIVATE -msimd128 -mrelaxed-simd -msse -msse2 -msse3 -mavx)
elseif(NOT MSVC)
target_compile_options(UtilyBenchmark PRIVATE "-Wall" "-Wextra" "-Wpedantic" "-Wcast-align" "-Wcast-qual" "-Wctor-dtor-privacy" "-Wformat=2" "-Winit-self" "-Wmissing-declarations" "-Wmissing-include-dirs" "-Wold-style-cast" "-Woverloaded-virtual" "-Wredundant-decls" "-Wshadow" "-Wsign-conversion" "-Wsign-promo" "-Wstrict-overflow=5" "-Wswitch-default" "-Wundef" "-Wno-unused" "-Wconversion" "-Wsign-compare")
target_compile_options(UtilyBenchmark PRIVATE -march=native)
target_compile_options(UtilyBenchmark PRIVATE -mtune=native)
target_compile_options(UtilyBenchmark PRIVATE
$<$<CONFIG:Release>:-O3 -DNDEBUG>
)
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,44 @@ constexpr auto to_array(Args&&... args)

</details>

## Performance

<details><summary><b>Simd128::Char</b></summary>

**Windows MSVC**

| String Operation | Std | StringZilla | Utily |
| ----------------------- | ------- | ----------- | ----------- |
| find | 13.2 ns | 109 ns | **11.3 ns** |
| find_first_of | 837 ns | | **46.2 ns** |
| find_substring(char[4]) | 192 ns | 609 ns | **58.4 ns** |
| find_substring(char[8]) | 186 ns | 1842 ns | **65.6 ns** |

*NOTE: StringZilla's method for SIMD seems be ignored by MSVC*

**Clang (Server)**

| String Operation | Std | StringZilla | Utily |
| ----------------------- | --- | ----------- | ----- |
| find | | | |
| find_first_of | | | |
| find_substring(char[4]) | | | |
| find_substring(char[8]) | | | |


**Emscripten**

| String Operation | Std | StringZilla | Utily |
| ----------------------- | --- | ----------- | ----- |
| find | | | |
| find_first_of | | | |
| find_substring(char[4]) | | | |
| find_substring(char[8]) | | | |

*NOTE: Only 128bit vector operations supported. *

</details>

## Get Started

<details><summary><b>Modern CMake</b></summary>
Expand Down
70 changes: 52 additions & 18 deletions benchmark/BenchSimd.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// #define UTY_NO_SIMD
// #define UTY_USE_SIMD_128
// #define UTY_USE_SIMD_256
// #define UTY_USE_SIMD_512

#ifndef EMSCRIPTEN
#include "stringzilla.h"
#endif

#include "Utily/Utily.hpp"

Expand All @@ -12,7 +12,7 @@
#include <numeric>
#include <ranges>

#if 1
#if 1

auto all_a(size_t max) -> std::string {
std::string v;
Expand All @@ -22,7 +22,7 @@ auto all_a(size_t max) -> std::string {
v.append("stringer");
return v;
}
const static std::string LONG_STRING = all_a(10000);
const static std::string LONG_STRING = all_a(1000);

auto iota(size_t max) -> std::vector<int32_t> {
std::vector<int32_t> v;
Expand All @@ -32,7 +32,6 @@ auto iota(size_t max) -> std::vector<int32_t> {
}
const static std::vector<int32_t> NUMS = iota(10000);


static void BM_Uty_find_char(benchmark::State& state) {
for (auto _ : state) {
volatile auto iter = Utily::Simd128::Char::find(LONG_STRING.data(), LONG_STRING.size(), 'z');
Expand All @@ -41,6 +40,17 @@ static void BM_Uty_find_char(benchmark::State& state) {
}
BENCHMARK(BM_Uty_find_char);

#ifndef EMSCRIPTEN
static void BM_Zil_find_char(benchmark::State& state) {
auto delim = 'z';
for (auto _ : state) {
volatile auto iter = sz_find_1char(LONG_STRING.data(), LONG_STRING.size(), &delim);
benchmark::DoNotOptimize(iter);
}
}
BENCHMARK(BM_Zil_find_char);
#endif

static void BM_Std_find_char(benchmark::State& state) {
for (auto _ : state) {
volatile auto iter = std::find(LONG_STRING.begin(), LONG_STRING.end(), 'z');
Expand All @@ -49,7 +59,6 @@ static void BM_Std_find_char(benchmark::State& state) {
}
BENCHMARK(BM_Std_find_char);


static void BM_Uty_find_first_of_char(benchmark::State& state) {
const auto data = std::to_array({ 'z', 'o', 'n' });
for (auto _ : state) {
Expand All @@ -59,6 +68,7 @@ static void BM_Uty_find_first_of_char(benchmark::State& state) {
}
BENCHMARK(BM_Uty_find_first_of_char);


static void BM_Std_find_first_of_chars(benchmark::State& state) {
const auto data = std::to_array({ 'z', 'o', 'n' });
for (auto _ : state) {
Expand All @@ -80,6 +90,20 @@ static void BM_Uty_search_char_4letters(benchmark::State& state) {
}
}
BENCHMARK(BM_Uty_search_char_4letters);
#ifndef EMSCRIPTEN
static void BM_Zil_search_char_4letters(benchmark::State& state) {
std::string_view find = "stri";
for (auto _ : state) {
volatile auto iter = sz_find_substring(
LONG_STRING.data(),
LONG_STRING.size(),
find.data(),
find.size());
benchmark::DoNotOptimize(iter);
}
}
BENCHMARK(BM_Zil_search_char_4letters);
#endif

static void BM_Std_search_char_4letters(benchmark::State& state) {
std::string_view find = "stri";
Expand All @@ -94,20 +118,30 @@ static void BM_Std_search_char_4letters(benchmark::State& state) {
}
BENCHMARK(BM_Std_search_char_4letters);


static void BM_Uty_search_char_8letters(benchmark::State& state) {
std::string_view find = "stringer";
for (auto _ : state) {
volatile auto iter = Utily::Simd128::Char::search(
LONG_STRING.data(),
LONG_STRING.size(),
find.data(),
find.size());
benchmark::DoNotOptimize(iter);
}
std::string_view find = "stringer";
for (auto _ : state) {
volatile auto index = Utily::Simd128::Char::search(
LONG_STRING.data(),
LONG_STRING.size(),
find.data(),
find.size());
benchmark::DoNotOptimize(index);
}
}
BENCHMARK(BM_Uty_search_char_8letters);

#ifndef EMSCRIPTEN
static void BM_Zil_search_char_8letters(benchmark::State& state) {
std::string_view find = "stringer";
for (auto _ : state) {
auto index = sz_find_substring(LONG_STRING.data(), LONG_STRING.size(), find.data(), find.size());
benchmark::DoNotOptimize(index);
}
}
BENCHMARK(BM_Zil_search_char_8letters);
#endif

static void BM_Std_search_char_8letters(benchmark::State& state) {
std::string_view find = "stringer";
for (auto _ : state) {
Expand Down
Loading

0 comments on commit 044ce27

Please sign in to comment.