1 /* Measure memmem functions.
2 Copyright (C) 2013-2015 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 500
23 #include "bench-string.h"
25 typedef char *(*proto_t
) (const void *, size_t, const void *, size_t);
26 void *simple_memmem (const void *, size_t, const void *, size_t);
28 IMPL (simple_memmem
, 0)
32 simple_memmem (const void *haystack
, size_t haystack_len
, const void *needle
,
36 const char *const last_possible
37 = (const char *) haystack
+ haystack_len
- needle_len
;
40 /* The first occurrence of the empty string is deemed to occur at
41 the beginning of the string. */
42 return (void *) haystack
;
44 /* Sanity check, otherwise the loop might search through the whole
46 if (__glibc_unlikely (haystack_len
< needle_len
))
49 for (begin
= (const char *) haystack
; begin
<= last_possible
; ++begin
)
50 if (begin
[0] == ((const char *) needle
)[0] &&
51 !memcmp ((const void *) &begin
[1],
52 (const void *) ((const char *) needle
+ 1),
54 return (void *) begin
;
60 do_one_test (impl_t
*impl
, const void *haystack
, size_t haystack_len
,
61 const void *needle
, size_t needle_len
, const void *expected
)
63 size_t i
, iters
= INNER_LOOP_ITERS
;
64 timing_t start
, stop
, cur
;
67 for (i
= 0; i
< iters
; ++i
)
69 CALL (impl
, haystack
, haystack_len
, needle
, needle_len
);
73 TIMING_DIFF (cur
, start
, stop
);
75 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
79 do_test (const char *str
, size_t len
, size_t idx
)
83 memcpy (tmpbuf
, buf1
+ idx
, len
);
84 memcpy (buf1
+ idx
, str
, len
);
86 printf ("String %s, offset %zd:", str
, idx
);
88 FOR_EACH_IMPL (impl
, 0)
89 do_one_test (impl
, buf1
, BUF1PAGES
* page_size
, str
, len
, buf1
+ idx
);
91 memcpy (buf1
+ idx
, tmpbuf
, len
);
97 do_random_tests (void)
99 for (size_t n
= 0; n
< ITERATIONS
; ++n
)
103 size_t shift
= random () % 11;
104 size_t rel
= random () % ((2 << (shift
+ 1)) * 64);
105 size_t idx
= MIN ((2 << shift
) * 64 + rel
, BUF1PAGES
* page_size
- 2);
106 size_t len
= random () % (sizeof (tmpbuf
) - 1) + 1;
107 len
= MIN (len
, BUF1PAGES
* page_size
- idx
- 1);
108 memcpy (tmpbuf
, buf1
+ idx
, len
);
109 for (size_t i
= random () % len
/ 2 + 1; i
> 0; --i
)
111 size_t off
= random () % len
;
112 char ch
= '0' + random () % 10;
114 buf1
[idx
+ off
] = ch
;
117 printf ("String %.*s, offset %zd:", (int) len
, buf1
+ idx
, idx
);
119 FOR_EACH_IMPL (impl
, 0)
120 do_one_test (impl
, buf1
, BUF1PAGES
* page_size
, buf1
+ idx
, len
,
125 memcpy (buf1
+ idx
, tmpbuf
, len
);
129 static const char *const strs
[] =
131 "00000", "00112233", "0123456789", "0000111100001111",
132 "00000111110000022222", "012345678901234567890",
133 "abc0", "aaaa0", "abcabc0"
145 FOR_EACH_IMPL (impl
, 0)
146 printf ("\t%s", impl
->name
);
149 for (i
= 0; i
< BUF1PAGES
* page_size
; ++i
)
150 buf1
[i
] = 60 + random () % 32;
152 for (i
= 0; i
< sizeof (strs
) / sizeof (strs
[0]); ++i
)
153 for (size_t j
= 0; j
< 120; j
+= 7)
155 size_t len
= strlen (strs
[i
]);
157 do_test (strs
[i
], len
, j
);
164 #include "../test-skeleton.c"