1 /* Measure STRCHR functions.
2 Copyright (C) 2013 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 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
24 # define TEST_NAME "strchr"
27 # define TEST_NAME "wcschr"
29 #include "bench-string.h"
32 # ifdef USE_FOR_STRCHRNUL
33 # define STRCHR strchrnul
34 # define stupid_STRCHR stupid_STRCHRNUL
35 # define simple_STRCHR simple_STRCHRNUL
37 # define STRCHR strchr
39 # define STRLEN strlen
41 # define BIG_CHAR CHAR_MAX
42 # define MIDDLE_CHAR 127
43 # define SMALL_CHAR 23
44 # define UCHAR unsigned char
47 # define STRCHR wcschr
48 # define STRLEN wcslen
50 # define BIG_CHAR WCHAR_MAX
51 # define MIDDLE_CHAR 1121
52 # define SMALL_CHAR 851
53 # define UCHAR wchar_t
56 #ifdef USE_FOR_STRCHRNUL
57 # define NULLRET(endptr) endptr
59 # define NULLRET(endptr) NULL
63 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
66 simple_STRCHR (const CHAR
*s
, int c
)
68 for (; *s
!= (CHAR
) c
; ++s
)
70 return NULLRET ((CHAR
*) s
);
75 stupid_STRCHR (const CHAR
*s
, int c
)
77 size_t n
= STRLEN (s
) + 1;
81 return (CHAR
*) s
- 1;
82 return NULLRET ((CHAR
*) s
- 1);
85 IMPL (stupid_STRCHR
, 0)
86 IMPL (simple_STRCHR
, 0)
90 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
94 hp_timing_t start
__attribute ((unused
));
95 hp_timing_t stop
__attribute ((unused
));
96 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
99 for (i
= 0; i
< 32; ++i
)
101 HP_TIMING_NOW (start
);
103 HP_TIMING_NOW (stop
);
104 HP_TIMING_BEST (best_time
, start
, stop
);
107 printf ("\t%zd", (size_t) best_time
);
112 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
113 /* For wcschr: align here means align not in bytes,
114 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
115 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
119 CHAR
*buf
= (CHAR
*) buf1
;
121 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
124 for (i
= 0; i
< len
; ++i
)
126 buf
[align
+ i
] = 32 + 23 * i
% max_char
;
127 if (buf
[align
+ i
] == seek_char
)
128 buf
[align
+ i
] = seek_char
+ 1;
129 else if (buf
[align
+ i
] == 0)
132 buf
[align
+ len
] = 0;
136 buf
[align
+ pos
] = seek_char
;
137 result
= buf
+ align
+ pos
;
139 else if (seek_char
== 0)
140 result
= buf
+ align
+ len
;
142 result
= NULLRET (buf
+ align
+ len
);
145 printf ("Length %4zd, alignment in bytes %2zd:",
146 pos
, align
* sizeof (CHAR
));
148 FOR_EACH_IMPL (impl
, 0)
149 do_one_test (impl
, buf
+ align
, seek_char
, result
);
163 FOR_EACH_IMPL (impl
, 0)
164 printf ("\t%s", impl
->name
);
167 for (i
= 1; i
< 8; ++i
)
169 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
170 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
173 for (i
= 1; i
< 8; ++i
)
175 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
176 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
179 for (i
= 0; i
< 32; ++i
)
181 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
182 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
185 for (i
= 1; i
< 8; ++i
)
187 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
188 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
191 for (i
= 1; i
< 8; ++i
)
193 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
194 do_test (i
, 64, 256, 0, BIG_CHAR
);
197 for (i
= 0; i
< 32; ++i
)
199 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
200 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
206 #include "../test-skeleton.c"