forked from attractivechaos/klib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkstring_bench2.c
More file actions
131 lines (124 loc) · 3.02 KB
/
Copy pathkstring_bench2.c
File metadata and controls
131 lines (124 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "kstring.h"
#ifdef __APPLE__
#define HAVE_STRNSTR
#endif
#ifdef __linux__
#define HAVE_MEMMEM
#endif
static int str_len = 1024*1024*128;
static int pat_len = 30;
static int alphabet = 2;
static int repeat = 50;
char *gen_data(int len, int a)
{
char *data;
int i;
long x;
srand48(11);
data = malloc(len);
for (i = 0; i < len; ++i)
data[i] = (int)(a * drand48()) + '!';
data[str_len - 1] = 0;
return data;
}
// http://srcvault.scali.eu.org/cgi-bin/Syntax/c/BoyerMoore.c
char *BoyerMoore( unsigned char *data, unsigned int dataLength, unsigned char *string, unsigned int strLength )
{
unsigned int skipTable[256], i;
unsigned char *search;
register unsigned char lastChar;
if (strLength == 0)
return NULL;
for (i = 0; i < 256; i++)
skipTable[i] = strLength;
search = string;
i = --strLength;
do {
skipTable[*search++] = i;
} while (i--);
lastChar = *--search;
search = data + strLength;
dataLength -= strLength+(strLength-1);
while ((int)dataLength > 0 ) {
unsigned int skip;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];
search += skip;
dataLength -= skip;
skip = skipTable[*search];
if (*search != lastChar) {
search += skip;
dataLength -= skip;
continue;
}
i = strLength;
do {
if (i-- == 0) return search;
} while (*--search == string[i]);
search += (strLength - i + 1);
dataLength--;
}
return NULL;
}
int main()
{
char *data;
int i;
clock_t t;
t = clock();
data = gen_data(str_len, alphabet);
fprintf(stderr, "Generate data in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
{
t = clock(); srand48(1331);
for (i = 0; i < repeat; ++i) {
int y = lrand48() % (str_len - pat_len);
char *ret;
ret = kmemmem(data, str_len, data + y, pat_len, 0);
// printf("%d, %d\n", (int)(ret - data), y);
}
fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}
if (1) {
t = clock(); srand48(1331);
for (i = 0; i < repeat; ++i) {
int y = lrand48() % (str_len - pat_len);
char *ret;
ret = BoyerMoore(data, str_len, data + y, pat_len);
// printf("%d, %d\n", (int)(ret - data), y);
}
fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}
#ifdef HAVE_STRNSTR
if (1) {
char *tmp;
t = clock(); srand48(1331);
tmp = calloc(pat_len+1, 1);
for (i = 0; i < repeat; ++i) {
int y = lrand48() % (str_len - pat_len);
char *ret;
memcpy(tmp, data + y, pat_len);
ret = strnstr(data, tmp, str_len);
}
fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}
#endif
#ifdef HAVE_MEMMEM
if (1) {
t = clock(); srand48(1331);
for (i = 0; i < repeat; ++i) {
int y = lrand48() % (str_len - pat_len);
char *ret;
ret = memmem(data, str_len, data + y, pat_len);
// printf("%d, %d\n", (int)(ret - data), y);
}
fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}
#endif
return 0;
}