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