regex bench from libc-bench
[libc-test] / src / regex / bench.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <regex.h>
6 #include <locale.h>
7 #include "test.h"
8
9
10 void bench_regex_compile(int N) {
11         regex_t re;
12         int i;
13
14         setlocale(LC_CTYPE, "");
15         for (i=0; i<N; i++) {
16                 regcomp(&re, "(a|b|c)*d*b", REG_EXTENDED);
17                 regfree(&re);
18         }
19 }
20
21 static void regex_search(int N, char *s) {
22         char buf[100000];
23         regex_t re;
24         int i;
25
26         setlocale(LC_CTYPE, "");
27         memset(buf, 'a', sizeof(buf)-2);
28         buf[sizeof buf - 2] = 'b';
29         buf[sizeof buf - 1] = 0;
30         regcomp(&re, s, REG_EXTENDED);
31         for (i=0; i<N; i++)
32                 regexec(&re, buf, 0, 0, 0);
33         regfree(&re);
34 }
35
36 void bench_regex_search1(int N) {
37         regex_search(N, "(a|b|c)*d*b");
38 }
39
40 void bench_regex_search2(int N) {
41         regex_search(N, "a{25}b");
42 }