This is needed to avoid the compiler eliding the call in highly-optimised builds.
55 lines
1.1 KiB
C
55 lines
1.1 KiB
C
/*
|
|
* 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());
|
|
|
|
volatile bool match;
|
|
START_CLOCK();
|
|
match = fsa_accepts(fsa, s, LEN);
|
|
STOP_CLOCK();
|
|
(void)match;
|
|
}
|
|
|
|
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();
|
|
}
|