-
Notifications
You must be signed in to change notification settings - Fork 3
/
naive.c
48 lines (44 loc) · 1.14 KB
/
naive.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "strtolh.h"
int main(int argc, char** argv) {
clock_t start, now;
const char ** strings;
int a = 1, verbose = 0;
long i, multi = 1, n = 1000;
for (a=1;argc>a;++a) {
if (strcmp(argv[a], "-v")==0) verbose = 1;
else if (strcmp(argv[a], "-x")==0) multi = strtolh(argv[++a], 10);
else {
n = strtolh(argv[a], 10);
break;
}
}
start = clock();
strings = (const char**)malloc(sizeof(const char *)*n);
for(i=0;i!=n;++i) {
char str[20];
sprintf(str, "%ld", i);
strings[i] = strdup(str);
}
now = clock();
printf("init: %.2fs\n", (float)(now-start)/CLOCKS_PER_SEC);
start = clock();
for (++a;a<argc;++a) {
const char * prefix = argv[a];
long m;
for (m=0;m!=multi;++m) {
long o = 0;
size_t len = strlen(prefix);
for(i=0;i!=n;++i) {
if (strncmp(prefix, strings[i], len)==0) ++o;
}
if (m==0 && verbose) printf("%ld matches for prefix %s in %ld strings\n", o, prefix, n);
}
}
now = clock();
printf("find: %.2fs\n", (float)(now-start)/CLOCKS_PER_SEC);
return 0;
}