bench-strlen.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Measure STRLEN functions.
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define TEST_MAIN
  16. #ifndef WIDE
  17. # define TEST_NAME "strlen"
  18. #else
  19. # define TEST_NAME "wcslen"
  20. # define generic_strlen generic_wcslen
  21. # define memchr_strlen wcschr_wcslen
  22. #endif
  23. #include "bench-string.h"
  24. #include "json-lib.h"
  25. typedef size_t (*proto_t) (const CHAR *);
  26. size_t generic_strlen (const CHAR *);
  27. size_t memchr_strlen (const CHAR *);
  28. IMPL (memchr_strlen, 0)
  29. IMPL (generic_strlen, 0)
  30. size_t
  31. memchr_strlen (const CHAR *p)
  32. {
  33. return (const CHAR *)MEMCHR (p, 0, PTRDIFF_MAX) - p;
  34. }
  35. IMPL (STRLEN, 1)
  36. static void
  37. do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t exp_len)
  38. {
  39. size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS_LARGE;
  40. timing_t start, stop, cur;
  41. if (len != exp_len)
  42. {
  43. error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
  44. len, exp_len);
  45. ret = 1;
  46. return;
  47. }
  48. TIMING_NOW (start);
  49. for (i = 0; i < iters; ++i)
  50. {
  51. CALL (impl, s);
  52. }
  53. TIMING_NOW (stop);
  54. TIMING_DIFF (cur, start, stop);
  55. json_element_double (json_ctx, (double) cur / (double) iters);
  56. }
  57. static void
  58. do_test (json_ctx_t *json_ctx, size_t align, size_t len)
  59. {
  60. size_t i;
  61. align &= 63;
  62. if (align + sizeof (CHAR) * len >= page_size)
  63. return;
  64. json_element_object_begin (json_ctx);
  65. json_attr_uint (json_ctx, "length", len);
  66. json_attr_uint (json_ctx, "alignment", align);
  67. json_array_begin (json_ctx, "timings");
  68. FOR_EACH_IMPL (impl, 0)
  69. {
  70. CHAR *buf = (CHAR *) (buf1);
  71. for (i = 0; i < len; ++i)
  72. buf[align + i] = 1 + 11111 * i % MAX_CHAR;
  73. buf[align + len] = 0;
  74. do_one_test (json_ctx, impl, (CHAR *) (buf + align), len);
  75. alloc_bufs ();
  76. }
  77. json_array_end (json_ctx);
  78. json_element_object_end (json_ctx);
  79. }
  80. int
  81. test_main (void)
  82. {
  83. json_ctx_t json_ctx;
  84. size_t i;
  85. test_init ();
  86. json_init (&json_ctx, 0, stdout);
  87. json_document_begin (&json_ctx);
  88. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  89. json_attr_object_begin (&json_ctx, "functions");
  90. json_attr_object_begin (&json_ctx, TEST_NAME);
  91. json_attr_string (&json_ctx, "bench-variant", "");
  92. json_array_begin (&json_ctx, "ifuncs");
  93. FOR_EACH_IMPL (impl, 0)
  94. json_element_string (&json_ctx, impl->name);
  95. json_array_end (&json_ctx);
  96. json_array_begin (&json_ctx, "results");
  97. /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
  98. for (i = 1; i < 8; ++i)
  99. {
  100. do_test (&json_ctx, sizeof (CHAR) * i, i);
  101. do_test (&json_ctx, 0, i);
  102. }
  103. for (i = 2; i <= 12; ++i)
  104. {
  105. do_test (&json_ctx, 0, 1 << i);
  106. do_test (&json_ctx, sizeof (CHAR) * 7, 1 << i);
  107. do_test (&json_ctx, sizeof (CHAR) * i, 1 << i);
  108. do_test (&json_ctx, sizeof (CHAR) * i, (size_t)((1 << i) / 1.5));
  109. }
  110. for (i = 0; i < 512; i += 32)
  111. {
  112. do_test (&json_ctx, 0, i / sizeof (CHAR));
  113. }
  114. for (i = 512; i < 1024; i += 64)
  115. {
  116. do_test (&json_ctx, 0, i / sizeof (CHAR));
  117. }
  118. for (i = 1024; i < 2048; i += 128)
  119. {
  120. do_test (&json_ctx, 0, i / sizeof (CHAR));
  121. }
  122. for (i = 2048; i < 4096; i += 256)
  123. {
  124. do_test (&json_ctx, 0, i / sizeof (CHAR));
  125. }
  126. for (i = 4096; i < 8192; i += 512)
  127. {
  128. do_test (&json_ctx, 0, i / sizeof (CHAR));
  129. }
  130. json_array_end (&json_ctx);
  131. json_attr_object_end (&json_ctx);
  132. json_attr_object_end (&json_ctx);
  133. json_document_end (&json_ctx);
  134. return ret;
  135. }
  136. #include <support/test-driver.c>
  137. #define libc_hidden_builtin_def(X)
  138. #ifndef WIDE
  139. # undef STRLEN
  140. # define STRLEN generic_strlen
  141. # include <string/strlen.c>
  142. #else
  143. # define WCSLEN generic_strlen
  144. # include <wcsmbs/wcslen.c>
  145. #endif