Write some matching benchmarks

This commit is contained in:
Camden Dixie O'Brien 2024-11-10 14:08:58 +00:00
parent e4d3b08bf2
commit 97529fdd2b
2 changed files with 56 additions and 0 deletions

View File

@ -14,3 +14,7 @@ function(add_benchmark_suites)
add_benchmark_suite(${source}) add_benchmark_suite(${source})
endforeach() endforeach()
endfunction() endfunction()
add_benchmark_suites(
matching_benchmarks.c
)

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "benchmarking.h"
#include "compile.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#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();
}