diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 9bc3e6d..473bfd3 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -14,3 +14,7 @@ function(add_benchmark_suites) add_benchmark_suite(${source}) endforeach() endfunction() + +add_benchmark_suites( + matching_benchmarks.c +) diff --git a/benchmarks/matching_benchmarks.c b/benchmarks/matching_benchmarks.c new file mode 100644 index 0000000..c98cc12 --- /dev/null +++ b/benchmarks/matching_benchmarks.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) Camden Dixie O'Brien + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include "benchmarking.h" +#include "compile.h" + +#include +#include +#include +#include + +#define LEN 100 +#define RANGE_FIRST 'a' +#define RANGE_LAST 'z' + +#define CLAMP_CHAR(x) (RANGE_FIRST + x % (RANGE_LAST - RANGE_FIRST + 1)) + +#define RUN_MATCHING_BENCHMARK(reps, name, regex) \ + do { \ + fsa_t fsa; \ + compile(regex, strlen(regex), &fsa); \ + RUN_BENCHMARK(reps, name, matching_benchmark, &fsa); \ + fsa_free(&fsa); \ + } while (0) + +static void matching_benchmark(const fsa_t *fsa) +{ + char s[LEN]; + for (int j = 0; j < LEN; ++j) + s[j] = CLAMP_CHAR(rand()); + + START_CLOCK(); + fsa_accepts(fsa, s, LEN); + STOP_CLOCK(); +} + +int main(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + srand(tv.tv_usec); + + BENCHMARKING_BEGIN(); + + RUN_MATCHING_BENCHMARK(10000, "foo or bar", ".*(foo|bar).*"); + RUN_MATCHING_BENCHMARK(10000, "regex #1", ".*(abc!?)*|dd+.*"); + RUN_MATCHING_BENCHMARK(10000, "regex #2", ".*(l|wh)?[aeiou]+.*"); + + return BENCHMARKING_END(); +}