[libcxx-commits] [libcxx] bb50aa7 - [libc++] Add some basic benchmarks for std::regex (#223633)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 18 02:34:37 PDT 2026
Author: Nikolas Klauser
Date: 2026-09-18T11:34:31+02:00
New Revision: bb50aa72c5eecafc831cc636006fcb926af35fa4
URL: https://github.com/llvm/llvm-project/commit/bb50aa72c5eecafc831cc636006fcb926af35fa4
DIFF: https://github.com/llvm/llvm-project/commit/bb50aa72c5eecafc831cc636006fcb926af35fa4.diff
LOG: [libc++] Add some basic benchmarks for std::regex (#223633)
Added:
libcxx/test/benchmarks/regex.bench.cpp
Modified:
Removed:
################################################################################
diff --git a/libcxx/test/benchmarks/regex.bench.cpp b/libcxx/test/benchmarks/regex.bench.cpp
new file mode 100644
index 0000000000000..9ebdff2466645
--- /dev/null
+++ b/libcxx/test/benchmarks/regex.bench.cpp
@@ -0,0 +1,97 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <regex>
+
+#include "benchmark/benchmark.h"
+#include "GenerateInput.h"
+#include "test_macros.h"
+
+template <std::regex_constants::syntax_option_type Arg>
+TEST_ALIGN_BENCHMARK static void BM_regex_construct(benchmark::State& state) {
+ static std::string_view regexes[] = {".*[abcdefghijklmnopqrtuvwxyz]{10,100}", "This is technically a regex."};
+ while (state.KeepRunningBatch(std::size(regexes))) {
+ for (auto& reg : regexes)
+ std::regex r(reg.data(), Arg);
+ }
+}
+BENCHMARK(BM_regex_construct<std::regex::basic>)->Name("std::regex(std::regex::basic)");
+BENCHMARK(BM_regex_construct<std::regex::extended>)->Name("std::regex(std::regex::extended)");
+BENCHMARK(BM_regex_construct<std::regex::awk>)->Name("std::regex(std::regex::awk)");
+BENCHMARK(BM_regex_construct<std::regex::ECMAScript>)->Name("std::regex(std::regex::ECMAScript)");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_bad_match(benchmark::State& state) {
+ std::regex r("This is technically a regex.");
+ std::string input = getRandomString(1 << 16);
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_bad_match)->Name("std::regex_search(\"This is technically a regex.\") (random data)");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_almost_match(benchmark::State& state) {
+ std::regex r("This is technically a regex.");
+ std::string input;
+ for (size_t i = 0; i != 2500; ++i)
+ input += "This is technically a rege";
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_almost_match)->Name("std::regex_search(\"This is technically a regex.\") (almost matches)");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_any_matcher(benchmark::State& state) {
+ std::regex r(".*");
+ std::string input;
+ input.append(1 << 16, 'a');
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_any_matcher)->Name("std::regex_search(\".*\")");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_alphabet_matcher(benchmark::State& state) {
+ std::regex r("[a-zA-Z]*");
+ std::string input;
+ input.append(1 << 16, 'a');
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_alphabet_matcher)->Name("std::regex_search(\"[a-zA-Z]*\")");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_alternation_loop_matcher(benchmark::State& state) {
+ std::regex r("(a|b)*c");
+ std::string input;
+ input.append(1 << 8, 'a');
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_alternation_loop_matcher)->Name("std::regex_search(\"(a|b)*c\")");
+
+TEST_ALIGN_BENCHMARK static void BM_regex_run_mail_matcher(benchmark::State& state) {
+ std::regex r(
+ R"regex((?:(?:[^<>()\[\].,;:\s@"]+(?:\.[^<>()\[\].,;:\s@"]+)*)|".+")@(?:(?:[^<>()\[\].,;:\s@"]+\.)+[^<>()\[\].,;:\s@"]{2,}))regex");
+ std::string input;
+ input.append(1 << 8, 'a');
+
+ for (auto _ : state) {
+ std::regex_search(input, r);
+ }
+}
+BENCHMARK(BM_regex_run_mail_matcher)->Name("std::regex_search(E-Mail matcher)");
+
+BENCHMARK_MAIN();
More information about the libcxx-commits
mailing list