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