]> git.ipfire.org Git - thirdparty/glibc.git/blame - benchtests/bench-strnlen.c
Improve strstr performance
[thirdparty/glibc.git] / benchtests / bench-strnlen.c
CommitLineData
97020474 1/* Measure strlen functions.
688903eb 2 Copyright (C) 2013-2018 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
fcf40ebe
SL
20#ifndef WIDE
21# define TEST_NAME "strnlen"
22#else
23# define TEST_NAME "wcsnlen"
24#endif /* WIDE */
97020474
SP
25#include "bench-string.h"
26
fcf40ebe
SL
27#ifndef WIDE
28# define STRNLEN strnlen
29# define CHAR char
30# define BIG_CHAR CHAR_MAX
31# define MIDDLE_CHAR 127
32# define SIMPLE_STRNLEN simple_strnlen
33#else
34# include <wchar.h>
35# define STRNLEN wcsnlen
36# define CHAR wchar_t
37# define BIG_CHAR WCHAR_MAX
38# define MIDDLE_CHAR 1121
39# define SIMPLE_STRNLEN simple_wcsnlen
40#endif /* WIDE */
41
42typedef size_t (*proto_t) (const CHAR *, size_t);
43size_t SIMPLE_STRNLEN (const CHAR *, size_t);
44
45IMPL (SIMPLE_STRNLEN, 0)
46IMPL (STRNLEN, 1)
97020474
SP
47
48size_t
fcf40ebe 49SIMPLE_STRNLEN (const CHAR *s, size_t maxlen)
97020474
SP
50{
51 size_t i;
52
53 for (i = 0; i < maxlen && s[i]; ++i);
54 return i;
55}
56
57static void
fcf40ebe 58do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
97020474 59{
44558701
WN
60 size_t len = CALL (impl, s, maxlen), i, iters = INNER_LOOP_ITERS;
61 timing_t start, stop, cur;
62
97020474
SP
63 if (len != exp_len)
64 {
65 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
66 len, exp_len);
67 ret = 1;
68 return;
69 }
70
44558701
WN
71 TIMING_NOW (start);
72 for (i = 0; i < iters; ++i)
97020474 73 {
44558701 74 CALL (impl, s, maxlen);
97020474 75 }
44558701
WN
76 TIMING_NOW (stop);
77
78 TIMING_DIFF (cur, start, stop);
79
80 TIMING_PRINT_MEAN ((double) cur, (double) iters);
97020474
SP
81}
82
83static void
84do_test (size_t align, size_t len, size_t maxlen, int max_char)
85{
86 size_t i;
87
fcf40ebe
SL
88 align &= 63;
89 if ((align + len) * sizeof (CHAR) >= page_size)
97020474
SP
90 return;
91
fcf40ebe
SL
92 CHAR *buf = (CHAR *) (buf1);
93
97020474 94 for (i = 0; i < len; ++i)
fcf40ebe
SL
95 buf[align + i] = 1 + 7 * i % max_char;
96 buf[align + len] = 0;
97020474 97
44558701 98 printf ("Length %4zd, alignment %2zd:", len, align);
97020474
SP
99
100 FOR_EACH_IMPL (impl, 0)
fcf40ebe 101 do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
97020474 102
44558701 103 putchar ('\n');
97020474
SP
104}
105
106int
107test_main (void)
108{
109 size_t i;
110
111 test_init ();
112
113 printf ("%20s", "");
114 FOR_EACH_IMPL (impl, 0)
115 printf ("\t%s", impl->name);
116 putchar ('\n');
117
118 for (i = 1; i < 8; ++i)
119 {
fcf40ebe
SL
120 do_test (0, i, i - 1, MIDDLE_CHAR);
121 do_test (0, i, i, MIDDLE_CHAR);
122 do_test (0, i, i + 1, MIDDLE_CHAR);
97020474
SP
123 }
124
125 for (i = 1; i < 8; ++i)
126 {
fcf40ebe
SL
127 do_test (i, i, i - 1, MIDDLE_CHAR);
128 do_test (i, i, i, MIDDLE_CHAR);
129 do_test (i, i, i + 1, MIDDLE_CHAR);
97020474
SP
130 }
131
132 for (i = 2; i <= 10; ++i)
133 {
fcf40ebe
SL
134 do_test (0, 1 << i, 5000, MIDDLE_CHAR);
135 do_test (1, 1 << i, 5000, MIDDLE_CHAR);
97020474
SP
136 }
137
138 for (i = 1; i < 8; ++i)
fcf40ebe 139 do_test (0, i, 5000, BIG_CHAR);
97020474
SP
140
141 for (i = 1; i < 8; ++i)
fcf40ebe 142 do_test (i, i, 5000, BIG_CHAR);
97020474
SP
143
144 for (i = 2; i <= 10; ++i)
145 {
fcf40ebe
SL
146 do_test (0, 1 << i, 5000, BIG_CHAR);
147 do_test (1, 1 << i, 5000, BIG_CHAR);
97020474
SP
148 }
149
150 return ret;
151}
152
b598e134 153#include <support/test-driver.c>