Hide __posix_spawn_file_actions_realloc/__spawni [BZ #18822]
[glibc.git] / benchtests / bench-strstr.c
blobe63659f13693f5d9a7a43bc0298e2229b9aeb783
1 /* Measure strstr functions.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strstr"
21 #include "bench-string.h"
24 #define STRSTR simple_strstr
25 #include "../string/strstr.c"
28 static char *
29 stupid_strstr (const char *s1, const char *s2)
31 ssize_t s1len = strlen (s1);
32 ssize_t s2len = strlen (s2);
34 if (s2len > s1len)
35 return NULL;
37 for (ssize_t i = 0; i <= s1len - s2len; ++i)
39 size_t j;
40 for (j = 0; j < s2len; ++j)
41 if (s1[i + j] != s2[j])
42 break;
43 if (j == s2len)
44 return (char *) s1 + i;
47 return NULL;
51 typedef char *(*proto_t) (const char *, const char *);
53 IMPL (stupid_strstr, 0)
54 IMPL (simple_strstr, 0)
55 IMPL (strstr, 1)
58 static void
59 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
61 size_t i, iters = INNER_LOOP_ITERS;
62 timing_t start, stop, cur;
64 TIMING_NOW (start);
65 for (i = 0; i < iters; ++i)
67 CALL (impl, s1, s2);
69 TIMING_NOW (stop);
71 TIMING_DIFF (cur, start, stop);
73 TIMING_PRINT_MEAN ((double) cur, (double) iters);
77 static void
78 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
79 int fail)
81 char *s1 = (char *) (buf1 + align1);
82 char *s2 = (char *) (buf2 + align2);
84 static const char d[] = "1234567890abcdef";
85 #define dl (sizeof (d) - 1)
86 char *ss2 = s2;
87 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
89 size_t t = l > dl ? dl : l;
90 ss2 = mempcpy (ss2, d, t);
92 s2[len2] = '\0';
94 if (fail)
96 char *ss1 = s1;
97 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
99 size_t t = l > dl ? dl : l;
100 memcpy (ss1, d, t);
101 ++ss1[len2 > 7 ? 7 : len2 - 1];
102 ss1 += t;
105 else
107 memset (s1, '0', len1);
108 memcpy (s1 + len1 - len2, s2, len2);
110 s1[len1] = '\0';
112 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
113 len1, len2, align1, align2, fail ? "fail" : "found");
115 FOR_EACH_IMPL (impl, 0)
116 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
118 putchar ('\n');
121 static int
122 test_main (void)
124 test_init ();
126 printf ("%23s", "");
127 FOR_EACH_IMPL (impl, 0)
128 printf ("\t%s", impl->name);
129 putchar ('\n');
131 for (size_t klen = 2; klen < 32; ++klen)
132 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
134 do_test (0, 0, hlen, klen, 0);
135 do_test (0, 0, hlen, klen, 1);
136 do_test (0, 3, hlen, klen, 0);
137 do_test (0, 3, hlen, klen, 1);
138 do_test (0, 9, hlen, klen, 0);
139 do_test (0, 9, hlen, klen, 1);
140 do_test (0, 15, hlen, klen, 0);
141 do_test (0, 15, hlen, klen, 1);
143 do_test (3, 0, hlen, klen, 0);
144 do_test (3, 0, hlen, klen, 1);
145 do_test (3, 3, hlen, klen, 0);
146 do_test (3, 3, hlen, klen, 1);
147 do_test (3, 9, hlen, klen, 0);
148 do_test (3, 9, hlen, klen, 1);
149 do_test (3, 15, hlen, klen, 0);
150 do_test (3, 15, hlen, klen, 1);
152 do_test (9, 0, hlen, klen, 0);
153 do_test (9, 0, hlen, klen, 1);
154 do_test (9, 3, hlen, klen, 0);
155 do_test (9, 3, hlen, klen, 1);
156 do_test (9, 9, hlen, klen, 0);
157 do_test (9, 9, hlen, klen, 1);
158 do_test (9, 15, hlen, klen, 0);
159 do_test (9, 15, hlen, klen, 1);
161 do_test (15, 0, hlen, klen, 0);
162 do_test (15, 0, hlen, klen, 1);
163 do_test (15, 3, hlen, klen, 0);
164 do_test (15, 3, hlen, klen, 1);
165 do_test (15, 9, hlen, klen, 0);
166 do_test (15, 9, hlen, klen, 1);
167 do_test (15, 15, hlen, klen, 0);
168 do_test (15, 15, hlen, klen, 1);
171 do_test (0, 0, page_size - 1, 16, 0);
172 do_test (0, 0, page_size - 1, 16, 1);
174 return ret;
177 #include <support/test-driver.c>