Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/test/kstring_bench2.c # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / test / kstring_bench2.c` 131 lines ยท 3092 bytes Raw Blame History 1 #include 2 #include 3 #include 4 #include 5 #include "kstring.h" 6 7 #ifdef __APPLE__ 8 #define HAVE_STRNSTR 9 #endif 10 11 #ifdef __linux__ 12 #define HAVE_MEMMEM 13 #endif 14 15 static int str_len = 1024*1024*128; 16 static int pat_len = 30; 17 static int alphabet = 2; 18 static int repeat = 50; 19 20 char *gen_data(int len, int a) 21 { 22 char *data; 23 int i; 24 long x; 25 srand48(11); 26 data = malloc(len); 27 for (i = 0; i < len; ++i) 28 data[i] = (int)(a * drand48()) + '!'; 29 data[str_len - 1] = 0; 30 return data; 31 } 32 // http://srcvault.scali.eu.org/cgi-bin/Syntax/c/BoyerMoore.c 33 char *BoyerMoore( unsigned char *data, unsigned int dataLength, unsigned char *string, unsigned int strLength ) 34 { 35 unsigned int skipTable[256], i; 36 unsigned char *search; 37 register unsigned char lastChar; 38 39 if (strLength == 0) 40 return NULL; 41 42 for (i = 0; i < 256; i++) 43 skipTable[i] = strLength; 44 search = string; 45 i = --strLength; 46 do { 47 skipTable[*search++] = i; 48 } while (i--); 49 lastChar = *--search; 50 search = data + strLength; 51 dataLength -= strLength+(strLength-1); 52 while ((int)dataLength > 0 ) { 53 unsigned int skip; 54 skip = skipTable[*search]; 55 search += skip; 56 dataLength -= skip; 57 skip = skipTable[*search]; 58 search += skip; 59 dataLength -= skip; 60 skip = skipTable[*search]; 61 if (*search != lastChar) { 62 search += skip; 63 dataLength -= skip; 64 continue; 65 } 66 i = strLength; 67 do { 68 if (i-- == 0) return search; 69 } while (*--search == string[i]); 70 search += (strLength - i + 1); 71 dataLength--; 72 } 73 return NULL; 74 } 75 76 int main() 77 { 78 char *data; 79 int i; 80 clock_t t; 81 t = clock(); 82 data = gen_data(str_len, alphabet); 83 fprintf(stderr, "Generate data in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); 84 { 85 t = clock(); srand48(1331); 86 for (i = 0; i < repeat; ++i) { 87 int y = lrand48() % (str_len - pat_len); 88 char *ret; 89 ret = kmemmem(data, str_len, data + y, pat_len, 0); 90 // printf("%d, %d\n", (int)(ret - data), y); 91 } 92 fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); 93 } 94 if (1) { 95 t = clock(); srand48(1331); 96 for (i = 0; i < repeat; ++i) { 97 int y = lrand48() % (str_len - pat_len); 98 char *ret; 99 ret = BoyerMoore(data, str_len, data + y, pat_len); 100 // printf("%d, %d\n", (int)(ret - data), y); 101 } 102 fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); 103 } 104 #ifdef HAVE_STRNSTR 105 if (1) { 106 char *tmp; 107 t = clock(); srand48(1331); 108 tmp = calloc(pat_len+1, 1); 109 for (i = 0; i < repeat; ++i) { 110 int y = lrand48() % (str_len - pat_len); 111 char *ret; 112 memcpy(tmp, data + y, pat_len); 113 ret = strnstr(data, tmp, str_len); 114 } 115 fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); 116 } 117 #endif 118 #ifdef HAVE_MEMMEM 119 if (1) { 120 t = clock(); srand48(1331); 121 for (i = 0; i < repeat; ++i) { 122 int y = lrand48() % (str_len - pat_len); 123 char *ret; 124 ret = memmem(data, str_len, data + y, pat_len); 125 // printf("%d, %d\n", (int)(ret - data), y); 126 } 127 fprintf(stderr, "Search patterns in %.3f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC); 128 } 129 #endif 130 return 0; 131 }