/* * 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(); }