]>
git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strcmp.c
f5e8b9cc95f40597ff321adeaa864ca01b6a96cc
1 /* Measure strcmp and wcscmp functions.
2 Copyright (C) 2013-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 # define TEST_NAME "wcscmp"
23 # define TEST_NAME "strcmp"
25 #include "bench-string.h"
28 # define L(str) L##str
29 # define SIMPLE_STRCMP simple_wcscmp
30 # define STUPID_STRCMP stupid_wcscmp
31 # define CHARBYTESLOG 2
32 # define MIDCHAR 0x7fffffff
33 # define LARGECHAR 0xfffffffe
35 /* Wcscmp uses signed semantics for comparison, not unsigned */
36 /* Avoid using substraction since possible overflow */
39 simple_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
51 return c1
< c2
? -1 : 1;
55 stupid_wcscmp (const wchar_t *s1
, const wchar_t *s2
)
57 size_t ns1
= wcslen (s1
) + 1;
58 size_t ns2
= wcslen (s2
) + 1;
59 size_t n
= ns1
< ns2
? ns1
: ns2
;
67 if ((ret
= c1
< c2
? -1 : c1
== c2
? 0 : 1) != 0)
77 # define SIMPLE_STRCMP simple_strcmp
78 # define STUPID_STRCMP stupid_strcmp
79 # define CHARBYTESLOG 0
81 # define LARGECHAR 0xfe
83 /* Strcmp uses unsigned semantics for comparison. */
85 simple_strcmp (const char *s1
, const char *s2
)
89 while ((ret
= *(unsigned char *) s1
- *(unsigned char*) s2
++) == 0 && *s1
++);
94 stupid_strcmp (const char *s1
, const char *s2
)
96 size_t ns1
= strlen (s1
) + 1;
97 size_t ns2
= strlen (s2
) + 1;
98 size_t n
= ns1
< ns2
? ns1
: ns2
;
102 if ((ret
= *(unsigned char *) s1
++ - *(unsigned char *) s2
++) != 0)
108 # include "json-lib.h"
110 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
112 IMPL (STUPID_STRCMP
, 1)
113 IMPL (SIMPLE_STRCMP
, 1)
117 do_one_test (json_ctx_t
*json_ctx
, impl_t
*impl
,
118 const CHAR
*s1
, const CHAR
*s2
,
121 size_t i
, iters
= INNER_LOOP_ITERS
;
122 timing_t start
, stop
, cur
;
125 for (i
= 0; i
< iters
; ++i
)
131 TIMING_DIFF (cur
, start
, stop
);
133 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
137 do_test (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len
, int
138 max_char
, int exp_result
)
148 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
152 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
155 /* Put them close to the end of page. */
156 i
= align1
+ CHARBYTES
* (len
+ 2);
157 s1
= (CHAR
*) (buf1
+ ((page_size
- i
) / 16 * 16) + align1
);
158 i
= align2
+ CHARBYTES
* (len
+ 2);
159 s2
= (CHAR
*) (buf2
+ ((page_size
- i
) / 16 * 16) + align2
);
161 for (i
= 0; i
< len
; i
++)
162 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
164 s1
[len
] = s2
[len
] = 0;
166 s2
[len
+ 1] = 24 + exp_result
;
167 s2
[len
- 1] -= exp_result
;
169 json_element_object_begin (json_ctx
);
170 json_attr_uint (json_ctx
, "length", (double) len
);
171 json_attr_uint (json_ctx
, "align1", (double) align1
);
172 json_attr_uint (json_ctx
, "align2", (double) align2
);
173 json_array_begin (json_ctx
, "timings");
175 FOR_EACH_IMPL (impl
, 0)
176 do_one_test (json_ctx
, impl
, s1
, s2
, exp_result
);
178 json_array_end (json_ctx
);
179 json_element_object_end (json_ctx
);
190 json_init (&json_ctx
, 0, stdout
);
192 json_document_begin (&json_ctx
);
193 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
195 json_attr_object_begin (&json_ctx
, "functions");
196 json_attr_object_begin (&json_ctx
, TEST_NAME
);
197 json_attr_string (&json_ctx
, "bench-variant", "default");
199 json_array_begin (&json_ctx
, "ifuncs");
200 FOR_EACH_IMPL (impl
, 0)
201 json_element_string (&json_ctx
, impl
->name
);
202 json_array_end (&json_ctx
);
204 json_array_begin (&json_ctx
, "results");
206 for (i
= 1; i
< 32; ++i
)
208 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0);
209 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1);
210 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1);
213 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
215 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, 0);
216 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, 0);
217 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, 1);
218 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, 1);
219 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, -1);
220 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, -1);
221 do_test (&json_ctx
, 0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1);
222 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
, LARGECHAR
, 1);
225 for (i
= 1; i
< 8; ++i
)
227 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 0);
228 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 0);
229 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, 1);
230 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, 1);
231 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
, MIDCHAR
, -1);
232 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
, LARGECHAR
, -1);
235 json_array_end (&json_ctx
);
236 json_attr_object_end (&json_ctx
);
237 json_attr_object_end (&json_ctx
);
238 json_document_end (&json_ctx
);
243 #include <support/test-driver.c>