diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2015-02-04 14:09:54 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2015-02-04 14:09:54 +0100 |
commit | bd82d030011cd8b9655e5ded6b6df9343b42a6bd (patch) | |
tree | de82d886dfea0cb7dbb6e80436218a25cb211bc3 /src/tc-memmem.c |
Imported Upstream version 3.22upstream/3.22
Diffstat (limited to 'src/tc-memmem.c')
-rw-r--r-- | src/tc-memmem.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tc-memmem.c b/src/tc-memmem.c new file mode 100644 index 0000000..9c59c7f --- /dev/null +++ b/src/tc-memmem.c @@ -0,0 +1,73 @@ +/* + * speed test HX_memmem + * Copyright Jan Engelhardt + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the WTF Public License version 2 or + * (at your option) any later version. + */ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <libHX/init.h> +#include <libHX/misc.h> +#include <libHX/string.h> + +static unsigned int size = 1048576 * 64; +static const char filler_text[] = + "Slhrdlu cringle tongle flonging blobbity bleep blingmangl"; + +static void long_scan(void) +{ + struct timespec start, stop, delta; + char *filler2, *p; + + filler2 = malloc(size); + if (filler2 == NULL) + abort(); + memset(filler2, 'l', size); + filler2[size-2] = 'a'; + + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start); + p = HX_memmem(filler2, strlen(filler2), "al", 2); + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop); + printf("long_scan: filler2=%p p=%p\n", filler2, p); + HX_timespec_sub(&delta, &stop, &start); + printf("long_scan: " HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&delta)); +} + +int main(void) +{ + unsigned int i; + char *haystack; + struct timespec start, stop, delta; + + if (HX_init() <= 0) + abort(); + haystack = malloc(size); + if (haystack == NULL) + abort(); + memset(haystack, 'A', size); + haystack[size-1] = haystack[size-2] = 'Z'; + printf("Init done\n"); + printf("Start=%p End=%p\n", filler_text, + filler_text + ARRAY_SIZE(filler_text)); + printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "nangl", 5)); + printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "angl", 4)); + printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "ngl", 3)); + printf("%p\n", HX_memmem(filler_text, strlen(filler_text), "ngl", 3)); + + long_scan(); + + for (i = 0; i < 10; ++i) { + printf("Search length %u...", i); + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start); + HX_memmem(haystack, size, haystack + size - i, i); + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop); + HX_timespec_sub(&delta, &stop, &start); + printf(HX_TIMESPEC_FMT "\n", HX_TIMESPEC_EXP(&delta)); + } + + HX_exit(); + return EXIT_SUCCESS; +} |