fcfc1e90782525647a73857046cdc9fe676165f7
[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         for (i=0; i<N; i++) {
21                 haystack[0]^=1;
22                 cs += (int)strstr(haystack, needle);
23         }
24         free(haystack);
25         return cs;
26 }
27
28 void bench_string_strstr1(int N) {
29         bstrstr(N, "abcdefghijklmnopqrstuvwxyz");
30 }
31 void bench_string_strstr2(int N) {
32         bstrstr(N, "azbycxdwevfugthsirjqkplomn");
33 }
34 void bench_string_strstr3(int N) {
35         bstrstr(N, "aaaaaaaaaaaaaacccccccccccc");
36 }
37 void bench_string_strstr4(int N) {
38         bstrstr(N, "aaaaaaaaaaaaaaaaaaaaaaaaac");
39 }
40 void bench_string_strstr5(int N) {
41         bstrstr(N, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac");
42 }
43
44 void bench_string_memset(int N) {
45         char *buf = malloc(LEN);
46         int i;
47
48         for (i=0; i<N; i++)
49                 memset(buf+i, i, LEN-i);
50         free(buf);
51 }
52
53 void bench_string_strchr(int N) {
54         char *buf = malloc(LEN);
55         int i;
56         int cs = 0;
57
58         memset(buf, 'a', LEN);
59         buf[LEN-1] = 0;
60         buf[LEN-2] = 'b';
61         reset_timer();
62         for (i=0; i<N; i++) {
63                 buf[i] = '0'+i%8;
64                 cs ^= (int)strchr(buf, 'b');
65         }
66         buf[0] = cs;
67         free(buf);
68 }
69
70 void bench_string_strlen(int N) {
71         char *buf = malloc(LEN);
72         int i;
73         int cs = 0;
74
75         memset(buf, 'a', LEN-1);
76         buf[LEN-1] = 0;
77         reset_timer();
78         for (i=0; i<N; i++) {
79                 buf[i] = '0'+i%8;
80                 cs ^= strlen(buf);
81         }
82         buf[0] = cs;
83         free(buf);
84 }