Created
February 8, 2023 20:30
-
-
Save wesley800/a5473034f99ec1dc2bec41b6baa1f3cc to your computer and use it in GitHub Desktop.
`strstr` and `strncmp+strlen` seems to be indistinguishable for is-prefix check.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
#define _WIN64 | |
#if defined(__GNUC__) | |
#include <x86intrin.h> | |
#elif defined(_MSC_VER) | |
#include <intrin.h> | |
#else | |
#error("???") | |
#endif | |
#define repeat10(x) \ | |
do { \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
{ x; } \ | |
} while (0) | |
#define BENCH(name, x) \ | |
do { \ | |
long times[10]; \ | |
double stddev = 0, avg = 0; \ | |
for (int j = 0; j < sizeof(times) / sizeof(times[0]); j++) { \ | |
long start = __rdtsc(); \ | |
for (int i = 0; i < 100000; i++) { \ | |
repeat10(repeat10(volatile int y = (x))); \ | |
} \ | |
long end = __rdtsc(); \ | |
times[j] = end - start; \ | |
avg += times[j]; \ | |
} \ | |
avg /= sizeof(times) / sizeof(times[0]); \ | |
for (int j = 0; j < sizeof(times) / sizeof(times[0]); j++) { \ | |
stddev += (times[j] - avg) * (times[j] - avg); \ | |
} \ | |
stddev /= sizeof(times) / sizeof(times[0]) - 1; \ | |
printf("%20s: avg=%15.3f std2=%15.3f\n", name, avg, sqrt(stddev)); \ | |
if (sqrt(stddev) > avg / 20) { \ | |
for (int j = 0; j < sizeof(times) / sizeof(times[0]); j++) { \ | |
printf(" %15.3ld", times[j]); \ | |
} \ | |
printf("\n"); \ | |
} \ | |
} while (0) | |
#define COMPARE(S, N) \ | |
do { \ | |
BENCH("strstr " #S " " #N, strstr(S, N) == S); \ | |
BENCH("strncmp " #S " " #N, !strncmp(S, N, strlen(N))); \ | |
} while (0) | |
const char S1[151] = "123456789012345678901234567890123456789012345678901234567" | |
"890123456789012345678901234567890123456789012345678901234" | |
"567890123456789012345678901234567890"; | |
const char S2[151] = "nociv1apipuaflc2c74wq1mys7js2tv0ghblue0np866p30r4rr0hp3eg" | |
"26m5btr56tgpr2dkhualaf3t7esfrqysitkaowrp2bs5z3q9z5bck3rn5" | |
"2bxi9a05gs0g623n6tt6iwf12xlnx68e7im0"; | |
const char N1[11] = "1234567890"; | |
const char N2[11] = "!@#$%^&*()"; | |
const char N3[11] = "26m5btr56t"; | |
const char N4[11] = "123456789!"; | |
const char N5[11] = "26m5btr56!"; | |
const char N6[141] = "123456789012345678901234567890123456789012345678901234567" | |
"890123456789012345678901234567890123456789012345678901234" | |
"56789012345678901234567890"; | |
const char N7[141] = "123456789012345678901234567890123456789012345678901234567" | |
"890123456789012345678901234567890123456789012345678901234" | |
"5678901234567890123456789!"; | |
int main() { | |
printf("Warming up...\n"); | |
COMPARE(S1, N1); | |
printf("Test:\n"); | |
COMPARE(S1, N1); | |
COMPARE(S1, N2); | |
COMPARE(S1, N3); | |
COMPARE(S1, N4); | |
COMPARE(S1, N5); | |
COMPARE(S1, N6); | |
COMPARE(S1, N7); | |
COMPARE(S2, N1); | |
COMPARE(S2, N2); | |
COMPARE(S2, N3); | |
COMPARE(S2, N4); | |
COMPARE(S2, N5); | |
COMPARE(S2, N6); | |
COMPARE(S2, N7); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment