]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strchr.c
benchtests: Enable BENCHSET to run subset of tests
[thirdparty/glibc.git] / benchtests / bench-strchr.c
1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
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 #ifndef WIDE
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
26 #else
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
29 # else
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
32 #endif /* WIDE */
33 #include "bench-string.h"
34
35 #ifndef WIDE
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
38 # define stupid_STRCHR stupid_STRCHRNUL
39 # define simple_STRCHR simple_STRCHRNUL
40 # else
41 # define STRCHR strchr
42 # endif /* !USE_FOR_STRCHRNUL */
43 # define STRLEN strlen
44 # define CHAR char
45 # define BIG_CHAR CHAR_MAX
46 # define MIDDLE_CHAR 127
47 # define SMALL_CHAR 23
48 # define UCHAR unsigned char
49 #else
50 # include <wchar.h>
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define stupid_STRCHR stupid_WCSCHRNUL
54 # define simple_STRCHR simple_WCSCHRNUL
55 # else
56 # define STRCHR wcschr
57 # endif /* !USE_FOR_STRCHRNUL */
58 # define STRLEN wcslen
59 # define CHAR wchar_t
60 # define BIG_CHAR WCHAR_MAX
61 # define MIDDLE_CHAR 1121
62 # define SMALL_CHAR 851
63 # define UCHAR wchar_t
64 #endif /* WIDE */
65
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
68 #else
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
71
72
73 typedef CHAR *(*proto_t) (const CHAR *, int);
74
75 CHAR *
76 simple_STRCHR (const CHAR *s, int c)
77 {
78 for (; *s != (CHAR) c; ++s)
79 if (*s == '\0')
80 return NULLRET ((CHAR *) s);
81 return (CHAR *) s;
82 }
83
84 CHAR *
85 stupid_STRCHR (const CHAR *s, int c)
86 {
87 size_t n = STRLEN (s) + 1;
88
89 while (n--)
90 if (*s++ == (CHAR) c)
91 return (CHAR *) s - 1;
92 return NULLRET ((CHAR *) s - 1);
93 }
94
95 IMPL (stupid_STRCHR, 0)
96 IMPL (simple_STRCHR, 0)
97 IMPL (STRCHR, 1)
98
99 static void
100 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
101 {
102 size_t i, iters = INNER_LOOP_ITERS;
103 timing_t start, stop, cur;
104
105 TIMING_NOW (start);
106 for (i = 0; i < iters; ++i)
107 {
108 CALL (impl, s, c);
109 }
110 TIMING_NOW (stop);
111
112 TIMING_DIFF (cur, start, stop);
113
114 TIMING_PRINT_MEAN ((double) cur, (double) iters);
115 }
116
117 static void
118 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
119 /* For wcschr: align here means align not in bytes,
120 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
121 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
122 {
123 size_t i;
124 CHAR *result;
125 CHAR *buf = (CHAR *) buf1;
126 align &= 15;
127 if ((align + len) * sizeof (CHAR) >= page_size)
128 return;
129
130 for (i = 0; i < len; ++i)
131 {
132 buf[align + i] = 32 + 23 * i % max_char;
133 if (buf[align + i] == seek_char)
134 buf[align + i] = seek_char + 1;
135 else if (buf[align + i] == 0)
136 buf[align + i] = 1;
137 }
138 buf[align + len] = 0;
139
140 if (pos < len)
141 {
142 buf[align + pos] = seek_char;
143 result = buf + align + pos;
144 }
145 else if (seek_char == 0)
146 result = buf + align + len;
147 else
148 result = NULLRET (buf + align + len);
149
150 printf ("Length %4zd, alignment in bytes %2zd:",
151 pos, align * sizeof (CHAR));
152
153 FOR_EACH_IMPL (impl, 0)
154 do_one_test (impl, buf + align, seek_char, result);
155
156 putchar ('\n');
157 }
158
159 int
160 test_main (void)
161 {
162 size_t i;
163
164 test_init ();
165
166 printf ("%20s", "");
167 FOR_EACH_IMPL (impl, 0)
168 printf ("\t%s", impl->name);
169 putchar ('\n');
170
171 for (i = 1; i < 8; ++i)
172 {
173 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
174 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
175 }
176
177 for (i = 1; i < 8; ++i)
178 {
179 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
180 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
181 }
182
183 for (i = 0; i < 32; ++i)
184 {
185 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
186 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
187 }
188
189 for (i = 1; i < 8; ++i)
190 {
191 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
192 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
193 }
194
195 for (i = 1; i < 8; ++i)
196 {
197 do_test (i, 64, 256, 0, MIDDLE_CHAR);
198 do_test (i, 64, 256, 0, BIG_CHAR);
199 }
200
201 for (i = 0; i < 32; ++i)
202 {
203 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
204 do_test (0, i, i + 1, 0, BIG_CHAR);
205 }
206
207 return ret;
208 }
209
210 #include <support/test-driver.c>