]> Savannah Git Hosting - gnulib.git/commitdiff
mbuiter: Add a benchmark.
authorBruno Haible <bruno@clisp.org>
Tue, 11 Jul 2023 19:54:54 +0000 (21:54 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 11 Jul 2023 19:54:54 +0000 (21:54 +0200)
* tests/bench-mbuiter.c: New file, based on tests/bench-mbiter.c.
* modules/mbuiter-bench-tests: New file.

ChangeLog
modules/mbuiter-bench-tests [new file with mode: 0644]
tests/bench-mbuiter.c [new file with mode: 0644]

index 7b2ec82db73a90043b38c1e8f100aaa55adf1d49..9b5ed4b15ba9bd76ca5edd84430264323937edcf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2023-07-11  Bruno Haible  <bruno@clisp.org>
 
+       mbuiter: Add a benchmark.
+       * tests/bench-mbuiter.c: New file, based on tests/bench-mbiter.c.
+       * modules/mbuiter-bench-tests: New file.
+
        mbiter: Add a benchmark.
        * tests/bench-mbiter.c: New file.
        * tests/bench-multibyte.h: New file.
diff --git a/modules/mbuiter-bench-tests b/modules/mbuiter-bench-tests
new file mode 100644 (file)
index 0000000..1e11c43
--- /dev/null
@@ -0,0 +1,18 @@
+Files:
+tests/bench-mbuiter.c
+tests/bench-multibyte.h
+tests/bench.h
+
+Depends-on:
+mbrtoc32-regular
+mbuiter
+setlocale
+striconv
+getrusage
+gettimeofday
+
+configure.ac:
+
+Makefile.am:
+noinst_PROGRAMS += bench-mbuiter
+bench_mbuiter_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) $(LIBC32CONV)
diff --git a/tests/bench-mbuiter.c b/tests/bench-mbuiter.c
new file mode 100644 (file)
index 0000000..0c24525
--- /dev/null
@@ -0,0 +1,133 @@
+/* Benchmarks for the mbuiter module.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <locale.h>
+#include <uchar.h>
+
+#include "bench.h"
+#include "bench-multibyte.h"
+#include "mbuiter.h"
+
+unsigned long long volatile result;
+
+static void
+do_test (char test, int repeat, const char *locale_name, const char *text)
+{
+  printf ("Test %c\n", test);
+  if (setlocale (LC_ALL, locale_name) != NULL)
+    {
+      struct timings_state ts;
+      timing_start (&ts);
+
+      int count;
+      for (count = 0; count < repeat; count++)
+        {
+          unsigned long long sum = 0;
+          mbui_iterator_t iter;
+          for (mbui_init (iter, text); mbui_avail (iter); mbui_advance (iter))
+            {
+              sum += mbui_cur (iter).wc;
+            }
+          result = sum;
+        }
+
+      timing_end (&ts);
+      timing_output (&ts);
+    }
+  else
+    {
+      printf ("Skipping test: locale %s not installed.\n", locale_name);
+    }
+  printf ("\n");
+}
+
+/* Performs some or all of the following tests:
+     a - ASCII text, C locale
+     b - ASCII text, UTF-8 locale
+     c - French text, C locale
+     d - French text, ISO-8859-1 locale
+     e - French text, UTF-8 locale
+     f - Greek text, C locale
+     g - Greek text, ISO-8859-7 locale
+     h - Greek text, UTF-8 locale
+     i - Chinese text, UTF-8 locale
+     j - Chinese text, GB18030 locale
+   Pass the tests to be performed as first argument.  */
+int
+main (int argc, char *argv[])
+{
+  if (argc != 3)
+    {
+      fprintf (stderr, "Usage: %s TESTS REPETITIONS\n", argv[0]);
+      fprintf (stderr, "Example: %s abcdefghij 100000\n", argv[0]);
+      exit (1);
+    }
+
+  const char *tests = argv[1];
+  int repeat = atoi (argv[2]);
+
+  text_init ();
+
+  /* Execute each test.  */
+  size_t i;
+  for (i = 0; i < strlen (tests); i++)
+    {
+      char test = tests[i];
+
+      switch (test)
+        {
+        case 'a':
+          do_test (test, repeat, "C", text_latin_ascii);
+          break;
+        case 'b':
+          do_test (test, repeat, "en_US.UTF-8", text_latin_ascii);
+          break;
+        case 'c':
+          do_test (test, repeat, "C", text_french_iso8859);
+          break;
+        case 'd':
+          do_test (test, repeat, "fr_FR.ISO-8859-1", text_french_iso8859);
+          break;
+        case 'e':
+          do_test (test, repeat, "en_US.UTF-8", text_french_utf8);
+          break;
+        case 'f':
+          do_test (test, repeat, "C", text_greek_iso8859);
+          break;
+        case 'g':
+          do_test (test, repeat, "el_GR.ISO-8859-7", text_greek_iso8859);
+          break;
+        case 'h':
+          do_test (test, repeat, "en_US.UTF-8", text_greek_utf8);
+          break;
+        case 'i':
+          do_test (test, repeat, "en_US.UTF-8", text_chinese_utf8);
+          break;
+        case 'j':
+          do_test (test, repeat, "zh_CN.GB18030", text_chinese_gb18030);
+          break;
+        default:
+          /* Ignore.  */
+          ;
+        }
+    }
+
+  return 0;
+}