[libcxx-commits] [libcxx] [libc++] Add some basic benchmarks for std::regex (PR #223633)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 15 02:11:41 PDT 2026
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/223633
None
>From bc63ab269c76af85e9077b1bf78732cad1b0b8bb Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 15 Sep 2026 11:10:56 +0200
Subject: [PATCH] [libc++] Add some basic benchmarks for std::regex
---
libcxx/test/benchmarks/regex.bench.cpp | 97 ++++++++++++++++++++++++++
1 file changed, 97 insertions(+)
create mode 100644 libcxx/test/benchmarks/regex.bench.cpp
diff --git a/libcxx/test/benchmarks/regex.bench.cpp b/libcxx/test/benchmarks/regex.bench.cpp
new file mode 100644
index 0000000000000..388eaf448ca62
--- /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
+
+#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>);
+BENCHMARK(BM_regex_construct<std::regex::extended>);
+BENCHMARK(BM_regex_construct<std::regex::awk>);
+BENCHMARK(BM_regex_construct<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);
+
+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);
+
+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);
+
+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);
+
+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);
+
+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);
+
+BENCHMARK_MAIN();
More information about the libcxx-commits
mailing list