File size: 10,353 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* Benchmark mcel and some alternatives
   Copyright 2023-2025 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 <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <uchar.h>

#include "bench.h"
#include "bench-multibyte.h"
#include "mbiter.h"
#include "mbiterf.h"
#include "mbuiter.h"
#include "mbuiterf.h"
#include "mcel.h"

typedef unsigned long long (*test_function) (char const *, char const *, int);

static unsigned long long
noop_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    {
      char const *iter;
      for (iter = text; iter < text_end; iter++)
        sum += (uintptr_t) iter;
    }

  return sum;
}

static unsigned long long
single_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    for (char const *iter = text; iter < text_end; )
      {
        unsigned char c = *iter++;
        sum += c;
      }

  return sum;
}

static unsigned long long
mbiter_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  size_t text_len = text_end - text;
  for (int count = 0; count < repeat; count++)
    {
      mbi_iterator_t iter;
      for (mbi_init (iter, text, text_len); mbi_avail (iter); )
        {
          mbchar_t cur = mbi_cur (iter);
          mbi_advance (iter);
          sum += cur.wc_valid ? cur.wc : (unsigned char) *mb_ptr (cur) << 16;
        }
    }

  return sum;
}

static unsigned long long
mbiterf_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    {
      mbif_state_t state;
      char const *iter;
      for (mbif_init (state), iter = text; mbif_avail (state, iter, text_end); )
        {
          mbchar_t cur = mbif_next (state, iter, text_end);
          iter += mb_len (cur);
          sum += cur.wc_valid ? cur.wc : (unsigned char) *mb_ptr (cur) << 16;
        }
    }

  return sum;
}

static unsigned long long
mbuiter_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    for (char const *t = text; t < text_end; t++)
      {
        mbui_iterator_t iter;
        for (mbui_init (iter, t); mbui_avail (iter); )
          {
            mbchar_t cur = mbui_cur (iter);
            mbui_advance (iter);
            sum += cur.wc_valid ? cur.wc : (unsigned char) *mb_ptr (cur) << 16;
          }
        t = mbui_cur_ptr (iter);
      }

  return sum;
}

static unsigned long long
mbuiterf_test (char const *text, _GL_UNUSED char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    for (char const *t = text; t < text_end; t++)
      {
        mbuif_state_t state;
        char const *iter;
        for (mbuif_init (state), iter = t; mbuif_avail (state, iter); )
          {
            mbchar_t cur = mbuif_next (state, iter);
            iter += mb_len (cur);
            sum += cur.wc_valid ? cur.wc : (unsigned char) *mb_ptr (cur) << 16;
          }
        t = iter;
      }

  return sum;
}

static unsigned long long
mcel_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    for (char const *iter = text; iter < text_end; )
      {
        mcel_t g = mcel_scan (iter, text_end);
        iter += g.len;
        sum += g.ch | (g.err << 16);
      }

  return sum;
}

static unsigned long long
mcuel_test (char const *text, char const *text_end, int repeat)
{
  unsigned long long sum = 0;

  for (int count = 0; count < repeat; count++)
    for (char const *t = text; t < text_end; t++)
      {
        char const *iter = t;
        while (*iter)
          {
            mcel_t g = mcel_scanz (iter);
            iter += g.len;
            sum += g.ch | (g.err << 16);
          }
        t = iter;
      }

  return sum;
}

static unsigned long long
do_1_test (test_function test, char const *text,
           char const *text_end, int repeat, struct timings_state *ts)
{
  timing_start (ts);
  unsigned long long sum = test (text, text_end, repeat);
  timing_end (ts);
  return sum;
}

static void
do_test (char test, int repeat, char const *locale_name,
         char const *text, size_t text_len)
{
  if (setlocale (LC_ALL, locale_name) != NULL)
    {
      char const *text_end = text + text_len;

      static struct
      {
        char const *name;
        test_function fn;
        struct timings_state ts;
        unsigned long long volatile sum;
      } testdesc[] = {
        { "noop", noop_test },
        { "single", single_test },
        { "mbiter", mbiter_test },
        { "mbiterf", mbiterf_test },
        { "mbuiter", mbuiter_test },
        { "mbuiterf", mbuiterf_test },
        { "mcel", mcel_test },
        { "mcuel", mcuel_test },
      };
      int ntestdesc = sizeof testdesc / sizeof *testdesc;
      for (int i = 0; i < ntestdesc; i++)
        testdesc[i].sum =
          do_1_test (testdesc[i].fn, text, text_end, repeat, &testdesc[i].ts);

      setlocale (LC_ALL, "C");

      static bool header_printed;
      if (!header_printed)
        {
          printf (" ");
          for (int i = 0; i < ntestdesc; i++)
            printf (" %8s", testdesc[i].name);
          printf ("\n");
          header_printed = true;
        }

      printf ("%c", test);
      for (int i = 0; i < ntestdesc; i++)
        {
          double user_usec = testdesc[i].ts.user_usec;
          double sys_usec = testdesc[i].ts.sys_usec;
          printf (" %8.3f", (user_usec + sys_usec) / 1e6);
        }
      printf ("\n");
    }
  else
    {
      printf ("Skipping test: locale %s not installed.\n", locale_name);
    }
}

/* 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
     K - Random bytes, C locale
     L - Random bytes, UTF-8 locale
     a - short ASCII text, C locale
     b - short ASCII text, UTF-8 locale
     e - short French text, UTF-8 locale
     h - short Greek text, UTF-8 locale
     i - short Chinese text, UTF-8 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 ABCDEFGHIJKLabehi 100000\n", argv[0]);
      exit (1);
    }

  char const *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,
                   strlen (text_latin_ascii));
          break;
        case 'a':
          do_test (test, repeat, "C", TEXT_LATIN_ASCII_LINE1,
                   strlen (TEXT_LATIN_ASCII_LINE1));
          break;
        case 'B':
          do_test (test, repeat, "en_US.UTF-8", text_latin_ascii,
                   strlen (text_latin_ascii));
          break;
        case 'b':
          do_test (test, repeat, "en_US.UTF-8", TEXT_LATIN_ASCII_LINE1,
                   strlen (TEXT_LATIN_ASCII_LINE1));
          break;
        case 'C':
          do_test (test, repeat, "C", text_french_iso8859,
                   strlen (text_french_iso8859));
          break;
        case 'D':
          do_test (test, repeat, "fr_FR.ISO-8859-1", text_french_iso8859,
                   strlen (text_french_iso8859));
          break;
        case 'E':
          do_test (test, repeat, "en_US.UTF-8", text_french_utf8,
                   strlen (text_french_utf8));
          break;
        case 'e':
          do_test (test, repeat, "en_US.UTF-8", TEXT_FRENCH_UTF8_LINE1,
                   strlen (TEXT_FRENCH_UTF8_LINE1));
          break;
        case 'F':
          do_test (test, repeat, "C", text_greek_iso8859,
                   strlen (text_greek_iso8859));
          break;
        case 'G':
          do_test (test, repeat, "el_GR.ISO-8859-7", text_greek_iso8859,
                   strlen (text_greek_iso8859));
          break;
        case 'H':
          do_test (test, repeat, "en_US.UTF-8", text_greek_utf8,
                   strlen (text_greek_utf8));
          break;
        case 'h':
          do_test (test, repeat, "en_US.UTF-8", TEXT_GREEK_UTF8_LINE1,
                   strlen (TEXT_GREEK_UTF8_LINE1));
          break;
        case 'I':
          do_test (test, repeat, "en_US.UTF-8", text_chinese_utf8,
                   strlen (text_chinese_utf8));
          break;
        case 'i':
          do_test (test, repeat, "en_US.UTF-8", TEXT_CHINESE_UTF8_LINE1,
                   strlen (TEXT_CHINESE_UTF8_LINE1));
          break;
        case 'J':
          do_test (test, repeat, "zh_CN.GB18030", text_chinese_gb18030,
                   strlen (text_chinese_gb18030));
          break;
        case 'K':
          do_test (test, repeat, "C", text_random_bytes,
                   sizeof text_random_bytes - 1);
          break;
        case 'L':
          do_test (test, repeat, "en_US.UTF-8", text_random_bytes,
                   sizeof text_random_bytes - 1);
          break;
        default:
          /* Ignore.  */
          ;
        }
    }

  return 0;
}