]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strspn.c
14a983744ef9dc80fffb763c3da39606508f800b
[thirdparty/glibc.git] / benchtests / bench-strspn.c
1 /* Measure strspn functions.
2 Copyright (C) 2013-2018 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 # define TEST_NAME "strspn"
22 #else
23 # define TEST_NAME "wcsspn"
24 #endif /* WIDE */
25 #include "bench-string.h"
26
27 #define BIG_CHAR MAX_CHAR
28
29 #ifndef WIDE
30 # define SIMPLE_STRSPN simple_strspn
31 # define STUPID_STRSPN stupid_strspn
32 # define SMALL_CHAR 127
33 #else
34 # define SIMPLE_STRSPN simple_wcsspn
35 # define STUPID_STRSPN stupid_wcsspn
36 # define SMALL_CHAR 1273
37 #endif /* WIDE */
38
39 typedef size_t (*proto_t) (const CHAR *, const CHAR *);
40 size_t SIMPLE_STRSPN (const CHAR *, const CHAR *);
41 size_t STUPID_STRSPN (const CHAR *, const CHAR *);
42
43 IMPL (STUPID_STRSPN, 0)
44 IMPL (SIMPLE_STRSPN, 0)
45 IMPL (STRSPN, 1)
46
47 size_t
48 SIMPLE_STRSPN (const CHAR *s, const CHAR *acc)
49 {
50 const CHAR *r, *str = s;
51 CHAR c;
52
53 while ((c = *s++) != '\0')
54 {
55 for (r = acc; *r != '\0'; ++r)
56 if (*r == c)
57 break;
58 if (*r == '\0')
59 return s - str - 1;
60 }
61 return s - str - 1;
62 }
63
64 size_t
65 STUPID_STRSPN (const CHAR *s, const CHAR *acc)
66 {
67 size_t ns = STRLEN (s), nacc = STRLEN (acc);
68 size_t i, j;
69
70 for (i = 0; i < ns; ++i)
71 {
72 for (j = 0; j < nacc; ++j)
73 if (s[i] == acc[j])
74 break;
75 if (j == nacc)
76 return i;
77 }
78 return i;
79 }
80
81 static void
82 do_one_test (impl_t *impl, const CHAR *s, const CHAR *acc, size_t exp_res)
83 {
84 size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS;
85 timing_t start, stop, cur;
86
87 if (res != exp_res)
88 {
89 error (0, 0, "Wrong result in function %s %p %p", impl->name,
90 (void *) res, (void *) exp_res);
91 ret = 1;
92 return;
93 }
94
95 TIMING_NOW (start);
96 for (i = 0; i < iters; ++i)
97 {
98 CALL (impl, s, acc);
99 }
100 TIMING_NOW (stop);
101
102 TIMING_DIFF (cur, start, stop);
103
104 TIMING_PRINT_MEAN ((double) cur, (double) iters);
105 }
106
107 static void
108 do_test (size_t align, size_t pos, size_t len)
109 {
110 size_t i;
111 CHAR *acc, *s;
112
113 align &= 7;
114 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || ! len)
115 return;
116
117 acc = (CHAR *) (buf2) + (random () & 255);
118 s = (CHAR *) (buf1) + align;
119
120 for (i = 0; i < len; ++i)
121 {
122 acc[i] = random () & BIG_CHAR;
123 if (!acc[i])
124 acc[i] = random () & BIG_CHAR;
125 if (!acc[i])
126 acc[i] = 1 + (random () & SMALL_CHAR);
127 }
128 acc[len] = '\0';
129
130 for (i = 0; i < pos; ++i)
131 s[i] = acc[random () % len];
132 s[pos] = random () & BIG_CHAR;
133 if (STRCHR (acc, s[pos]))
134 s[pos] = '\0';
135 else
136 {
137 for (i = pos + 1; i < pos + 10; ++i)
138 s[i] = random () & BIG_CHAR;
139 s[i] = '\0';
140 }
141
142 printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len);
143
144 FOR_EACH_IMPL (impl, 0)
145 do_one_test (impl, s, acc, pos);
146
147 putchar ('\n');
148 }
149
150 int
151 test_main (void)
152 {
153 size_t i;
154
155 test_init ();
156
157 printf ("%32s", "");
158 FOR_EACH_IMPL (impl, 0)
159 printf ("\t%s", impl->name);
160 putchar ('\n');
161
162 for (i = 0; i < 32; ++i)
163 {
164 do_test (0, 512, i);
165 do_test (i, 512, i);
166 }
167
168 for (i = 1; i < 8; ++i)
169 {
170 do_test (0, 16 << i, 4);
171 do_test (i, 16 << i, 4);
172 }
173
174 for (i = 1; i < 8; ++i)
175 do_test (i, 64, 10);
176
177 for (i = 0; i < 64; ++i)
178 do_test (0, i, 6);
179
180 return ret;
181 }
182
183 #include <support/test-driver.c>