]> git.ipfire.org Git - thirdparty/glibc.git/blame - benchtests/bench-memmem.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / benchtests / bench-memmem.c
CommitLineData
97020474 1/* Measure memmem functions.
b168057a 2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
97020474
SP
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#define TEST_MAIN
20#define TEST_NAME "memmem"
21#define BUF1PAGES 20
22#define ITERATIONS 500
23#include "bench-string.h"
24
25typedef char *(*proto_t) (const void *, size_t, const void *, size_t);
26void *simple_memmem (const void *, size_t, const void *, size_t);
27
28IMPL (simple_memmem, 0)
29IMPL (memmem, 1)
30
31void *
32simple_memmem (const void *haystack, size_t haystack_len, const void *needle,
33 size_t needle_len)
34{
35 const char *begin;
36 const char *const last_possible
37 = (const char *) haystack + haystack_len - needle_len;
38
39 if (needle_len == 0)
40 /* The first occurrence of the empty string is deemed to occur at
41 the beginning of the string. */
42 return (void *) haystack;
43
44 /* Sanity check, otherwise the loop might search through the whole
45 memory. */
a1ffb40e 46 if (__glibc_unlikely (haystack_len < needle_len))
97020474
SP
47 return NULL;
48
49 for (begin = (const char *) haystack; begin <= last_possible; ++begin)
50 if (begin[0] == ((const char *) needle)[0] &&
51 !memcmp ((const void *) &begin[1],
52 (const void *) ((const char *) needle + 1),
53 needle_len - 1))
54 return (void *) begin;
55
56 return NULL;
57}
58
59static void
60do_one_test (impl_t *impl, const void *haystack, size_t haystack_len,
61 const void *needle, size_t needle_len, const void *expected)
62{
44558701
WN
63 size_t i, iters = INNER_LOOP_ITERS;
64 timing_t start, stop, cur;
65
66 TIMING_NOW (start);
67 for (i = 0; i < iters; ++i)
97020474 68 {
44558701
WN
69 CALL (impl, haystack, haystack_len, needle, needle_len);
70 }
71 TIMING_NOW (stop);
97020474 72
44558701 73 TIMING_DIFF (cur, start, stop);
97020474 74
44558701 75 TIMING_PRINT_MEAN ((double) cur, (double) iters);
97020474
SP
76}
77
78static void
79do_test (const char *str, size_t len, size_t idx)
80{
81 char tmpbuf[len];
82
83 memcpy (tmpbuf, buf1 + idx, len);
84 memcpy (buf1 + idx, str, len);
85
44558701 86 printf ("String %s, offset %zd:", str, idx);
97020474
SP
87
88 FOR_EACH_IMPL (impl, 0)
89 do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx);
90
91 memcpy (buf1 + idx, tmpbuf, len);
92
44558701 93 putchar ('\n');
97020474
SP
94}
95
96static void
97do_random_tests (void)
98{
99 for (size_t n = 0; n < ITERATIONS; ++n)
100 {
101 char tmpbuf[32];
102
103 size_t shift = random () % 11;
104 size_t rel = random () % ((2 << (shift + 1)) * 64);
105 size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2);
106 size_t len = random () % (sizeof (tmpbuf) - 1) + 1;
107 len = MIN (len, BUF1PAGES * page_size - idx - 1);
108 memcpy (tmpbuf, buf1 + idx, len);
109 for (size_t i = random () % len / 2 + 1; i > 0; --i)
110 {
111 size_t off = random () % len;
112 char ch = '0' + random () % 10;
113
114 buf1[idx + off] = ch;
115 }
116
44558701 117 printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx);
97020474
SP
118
119 FOR_EACH_IMPL (impl, 0)
120 do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len,
121 buf1 + idx);
122
44558701 123 putchar ('\n');
97020474
SP
124
125 memcpy (buf1 + idx, tmpbuf, len);
126 }
127}
128
129static const char *const strs[] =
130 {
131 "00000", "00112233", "0123456789", "0000111100001111",
132 "00000111110000022222", "012345678901234567890",
133 "abc0", "aaaa0", "abcabc0"
134 };
135
136
137int
138test_main (void)
139{
140 size_t i;
141
142 test_init ();
143
144 printf ("%23s", "");
145 FOR_EACH_IMPL (impl, 0)
146 printf ("\t%s", impl->name);
147 putchar ('\n');
148
149 for (i = 0; i < BUF1PAGES * page_size; ++i)
150 buf1[i] = 60 + random () % 32;
151
152 for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i)
153 for (size_t j = 0; j < 120; j += 7)
154 {
155 size_t len = strlen (strs[i]);
156
157 do_test (strs[i], len, j);
158 }
159
160 do_random_tests ();
161 return ret;
162}
163
164#include "../test-skeleton.c"