fnmatch: fix "[/b" test
[libc-test] / src / string / bench.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "test.h"
5
6 #define LEN 200000
7
8 static int bstrstr(int N, const char *needle) {
9         size_t l = strlen(needle);
10         size_t cnt = 10000;
11         int i;
12         int cs = 0;
13         char *haystack = malloc(l * cnt + 1);
14
15         for (i=0; i<cnt-1; i++) {
16                 memcpy(haystack + l*i, needle, l);
17                 haystack[l*i+l-1] ^= 1;
18         }
19         memcpy(haystack + l*i, needle, l+1);
20         reset_timer();
21         for (i=0; i<N; i++) {
22                 haystack[0]^=1;
23                 cs += (int)strstr(haystack, needle);
24         }
25         free(haystack);
26         return cs;
27 }
28
29 void bench_string_strstr1(int N) {
30         bstrstr(N, "abcdefghijklmnopqrstuvwxyz");
31 }
32 void bench_string_strstr2(int N) {
33         bstrstr(N, "azbycxdwevfugthsirjqkplomn");
34 }
35 void bench_string_strstr3(int N) {
36         bstrstr(N, "aaaaaaaaaaaaaacccccccccccc");
37 }
38 void bench_string_strstr4(int N) {
39         bstrstr(N, "aaaaaaaaaaaaaaaaaaaaaaaaac");
40 }
41 void bench_string_strstr5(int N) {
42         bstrstr(N, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac");
43 }
44
45 void bench_string_memset(int N) {
46         char *buf = malloc(LEN);
47         int i;
48
49         for (i=0; i<N; i++)
50                 memset(buf+i, i, LEN-i);
51         free(buf);
52 }
53
54 void bench_string_strchr(int N) {
55         char *buf = malloc(LEN);
56         int i;
57         int cs = 0;
58
59         memset(buf, 'a', LEN);
60         buf[LEN-1] = 0;
61         buf[LEN-2] = 'b';
62         reset_timer();
63         for (i=0; i<N; i++) {
64                 buf[i] = '0'+i%8;
65                 cs ^= (int)strchr(buf, 'b');
66         }
67         buf[0] = cs;
68         free(buf);
69 }
70
71 void bench_string_strlen(int N) {
72         char *buf = malloc(LEN);
73         int i;
74         int cs = 0;
75
76         memset(buf, 'a', LEN-1);
77         buf[LEN-1] = 0;
78         reset_timer();
79         for (i=0; i<N; i++) {
80                 buf[i] = '0'+i%8;
81                 cs ^= strlen(buf);
82         }
83         buf[0] = cs;
84         free(buf);
85 }