]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strlen.c
Reallocate buffers for every run in strlen
[thirdparty/glibc.git] / benchtests / bench-strlen.c
1 /* Measure STRLEN 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 "strlen"
22 #else
23 # define TEST_NAME "wcslen"
24 #endif
25 #include "bench-string.h"
26
27 #ifndef WIDE
28 # define STRLEN strlen
29 # define CHAR char
30 # define MAX_CHAR CHAR_MAX
31 #else
32 # include <wchar.h>
33 # define STRLEN wcslen
34 # define CHAR wchar_t
35 # define MAX_CHAR WCHAR_MAX
36 #endif
37
38 #include "json-lib.h"
39
40 typedef size_t (*proto_t) (const CHAR *);
41
42 size_t
43 simple_STRLEN (const CHAR *s)
44 {
45 const CHAR *p;
46
47 for (p = s; *p; ++p);
48 return p - s;
49 }
50
51 #ifndef WIDE
52 size_t
53 builtin_strlen (const CHAR *p)
54 {
55 return __builtin_strlen (p);
56 }
57 IMPL (builtin_strlen, 0)
58 #endif
59
60 IMPL (simple_STRLEN, 0)
61 IMPL (STRLEN, 1)
62
63
64 static void
65 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t exp_len)
66 {
67 size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS;
68 timing_t start, stop, cur;
69
70 if (len != exp_len)
71 {
72 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
73 len, exp_len);
74 ret = 1;
75 return;
76 }
77
78 TIMING_NOW (start);
79 for (i = 0; i < iters; ++i)
80 {
81 CALL (impl, s);
82 }
83 TIMING_NOW (stop);
84
85 TIMING_DIFF (cur, start, stop);
86
87 json_element_double (json_ctx, (double) cur / (double) iters);
88 }
89
90 static void
91 do_test (json_ctx_t *json_ctx, size_t align, size_t len)
92 {
93 size_t i;
94
95 align &= 63;
96 if (align + sizeof(CHAR) * len >= page_size)
97 return;
98
99 json_element_object_begin (json_ctx);
100 json_attr_uint (json_ctx, "length", len);
101 json_attr_uint (json_ctx, "alignment", align);
102 json_array_begin (json_ctx, "timings");
103
104
105 FOR_EACH_IMPL (impl, 0)
106 {
107 CHAR *buf = (CHAR *) (buf1);
108
109 for (i = 0; i < len; ++i)
110 buf[align + i] = 1 + 11111 * i % MAX_CHAR;
111 buf[align + len] = 0;
112
113 do_one_test (json_ctx, impl, (CHAR *) (buf + align), len);
114 alloc_bufs ();
115 }
116
117 json_array_end (json_ctx);
118 json_element_object_end (json_ctx);
119 }
120
121 int
122 test_main (void)
123 {
124 json_ctx_t json_ctx;
125 size_t i;
126
127 test_init ();
128
129 json_init (&json_ctx, 0, stdout);
130
131 json_document_begin (&json_ctx);
132 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
133
134 json_attr_object_begin (&json_ctx, "functions");
135 json_attr_object_begin (&json_ctx, TEST_NAME);
136 json_attr_string (&json_ctx, "bench-variant", "");
137
138 json_array_begin (&json_ctx, "ifuncs");
139 FOR_EACH_IMPL (impl, 0)
140 json_element_string (&json_ctx, impl->name);
141 json_array_end (&json_ctx);
142
143 json_array_begin (&json_ctx, "results");
144 /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
145
146 for (i = 1; i < 8; ++i)
147 {
148 do_test (&json_ctx, sizeof(CHAR) * i, i);
149 do_test (&json_ctx, 0, i);
150 }
151
152 for (i = 2; i <= 12; ++i)
153 {
154 do_test (&json_ctx, 0, 1 << i);
155 do_test (&json_ctx, sizeof(CHAR) * 7, 1 << i);
156 do_test (&json_ctx, sizeof(CHAR) * i, 1 << i);
157 do_test (&json_ctx, sizeof(CHAR) * i, (size_t)((1 << i) / 1.5));
158 }
159
160 json_array_end (&json_ctx);
161 json_attr_object_end (&json_ctx);
162 json_attr_object_end (&json_ctx);
163 json_document_end (&json_ctx);
164
165 return ret;
166 }
167
168 #include <support/test-driver.c>