1 /* Measure memmem functions.
2 Copyright (C) 2013-2019 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/>. */
20 #define TEST_NAME "memmem"
22 #define ITERATIONS 100
23 #include "bench-string.h"
25 typedef char *(*proto_t
) (const void *, size_t, const void *, size_t);
28 basic_memmem (const void *haystack
, size_t hs_len
, const void *needle
,
31 const char *hs
= haystack
;
32 const char *ne
= needle
;
38 const char *end
= hs
+ hs_len
- ne_len
;
40 for ( ; hs
<= end
; hs
++)
44 for (i
= ne_len
- 1; i
!= 0; i
--)
54 #define RETURN_TYPE void *
55 #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
56 #define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
57 #include "../string/str-two-way.h"
60 twoway_memmem (const void *haystack_start
, size_t haystack_len
,
61 const void *needle_start
, size_t needle_len
)
63 /* Abstract memory is considered to be an array of 'unsigned char' values,
64 not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
65 const unsigned char *haystack
= (const unsigned char *) haystack_start
;
66 const unsigned char *needle
= (const unsigned char *) needle_start
;
69 /* The first occurrence of the empty string is deemed to occur at
70 the beginning of the string. */
71 return (void *) haystack
;
73 /* Sanity check, otherwise the loop might search through the whole
75 if (__glibc_unlikely (haystack_len
< needle_len
))
78 /* Use optimizations in memchr when possible, to reduce the search
79 size of haystack using a linear algorithm with a smaller
80 coefficient. However, avoid memchr for long needles, since we
81 can often achieve sublinear performance. */
82 if (needle_len
< LONG_NEEDLE_THRESHOLD
)
84 haystack
= memchr (haystack
, *needle
, haystack_len
);
85 if (!haystack
|| __builtin_expect (needle_len
== 1, 0))
86 return (void *) haystack
;
87 haystack_len
-= haystack
- (const unsigned char *) haystack_start
;
88 if (haystack_len
< needle_len
)
90 /* Check whether we have a match. This improves performance since we
91 avoid the initialization overhead of the two-way algorithm. */
92 if (memcmp (haystack
, needle
, needle_len
) == 0)
93 return (void *) haystack
;
94 return two_way_short_needle (haystack
, haystack_len
, needle
, needle_len
);
97 return two_way_long_needle (haystack
, haystack_len
, needle
, needle_len
);
101 IMPL (twoway_memmem
, 0)
102 IMPL (basic_memmem
, 0)
105 do_one_test (impl_t
*impl
, const void *haystack
, size_t haystack_len
,
106 const void *needle
, size_t needle_len
, const void *expected
)
108 size_t i
, iters
= INNER_LOOP_ITERS_SMALL
;
109 timing_t start
, stop
, cur
;
112 for (i
= 0; i
< iters
; ++i
)
114 CALL (impl
, haystack
, haystack_len
, needle
, needle_len
);
118 TIMING_DIFF (cur
, start
, stop
);
120 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
124 do_test (const char *str
, size_t len
, size_t idx
)
128 memcpy (tmpbuf
, buf1
+ idx
, len
);
129 memcpy (buf1
+ idx
, str
, len
);
131 printf ("String %s, offset %zd:", str
, idx
);
133 FOR_EACH_IMPL (impl
, 0)
134 do_one_test (impl
, buf1
, BUF1PAGES
* page_size
, str
, len
, buf1
+ idx
);
136 memcpy (buf1
+ idx
, tmpbuf
, len
);
142 do_random_tests (void)
144 for (size_t n
= 0; n
< ITERATIONS
; ++n
)
148 size_t shift
= random () % 11;
149 size_t rel
= random () % ((2 << (shift
+ 1)) * 64);
150 size_t idx
= MIN ((2 << shift
) * 64 + rel
, BUF1PAGES
* page_size
- 2);
151 size_t len
= random () % (sizeof (tmpbuf
) - 1) + 1;
152 len
= MIN (len
, BUF1PAGES
* page_size
- idx
- 1);
153 memcpy (tmpbuf
, buf1
+ idx
, len
);
154 for (size_t i
= random () % len
/ 2 + 1; i
> 0; --i
)
156 size_t off
= random () % len
;
157 char ch
= '0' + random () % 10;
159 buf1
[idx
+ off
] = ch
;
162 printf ("String %.*s, offset %zd:", (int) len
, buf1
+ idx
, idx
);
164 FOR_EACH_IMPL (impl
, 0)
165 do_one_test (impl
, buf1
, BUF1PAGES
* page_size
, buf1
+ idx
, len
,
170 memcpy (buf1
+ idx
, tmpbuf
, len
);
174 static const char *const strs
[] =
176 "00000", "00112233", "0123456789", "0000111100001111",
177 "00000111110000022222", "012345678901234567890",
178 "abc0", "aaaa0", "abcabc0"
190 FOR_EACH_IMPL (impl
, 0)
191 printf ("\t%s", impl
->name
);
194 for (i
= 0; i
< BUF1PAGES
* page_size
; ++i
)
195 buf1
[i
] = 60 + random () % 32;
197 for (i
= 0; i
< sizeof (strs
) / sizeof (strs
[0]); ++i
)
198 for (size_t j
= 0; j
< 120; j
+= 7)
200 size_t len
= strlen (strs
[i
]);
202 do_test (strs
[i
], len
, j
);
209 #include <support/test-driver.c>