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
|
/* -*- c-basic-offset: 2; coding: utf-8 -*- */
/*
Copyright (C) 2015-2016 Kouhei Sutou <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Groonga: ed300a833d44eaefa978b5ecf46a96ef91ae0891
CFLAGS: -O2 -g
% make --quiet -C benchmark run-bench-nfkc
run-bench-nfkc:
(total) (average) (median)
map1 - switch : (0.0060ms) (0.00060000ms) (0.00000000ms)
map1 - table : (0.00000000ms) (0.00000000ms) (0.00000000ms)
map2 - switch - no change: (0.0010ms) (0.00010000ms) (0.00000000ms)
map2 - table - no change: (0.00000000ms) (0.00000000ms) (0.00000000ms)
map2 - switch - change: (0.0010ms) (0.00010000ms) (0.00000000ms)
map2 - table - change: (0.0010ms) (0.00010000ms) (0.00000000ms)
*/
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <groonga.h>
#include "lib/benchmark.h"
#include "../lib/nfkc50.c"
#define MAX_UNICODE 0x110000
#define BUFFER_SIZE 0x100
static inline int
ucs2utf8(unsigned int i, unsigned char *buf)
{
unsigned char *p = buf;
if (i < 0x80) {
*p++ = i;
} else {
if (i < 0x800) {
*p++ = (i >> 6) | 0xc0;
} else {
if (i < 0x00010000) {
*p++ = (i >> 12) | 0xe0;
} else {
if (i < 0x00200000) {
*p++ = (i >> 18) | 0xf0;
} else {
if (i < 0x04000000) {
*p++ = (i >> 24) | 0xf8;
} else if (i < 0x80000000) {
*p++ = (i >> 30) | 0xfc;
*p++ = ((i >> 24) & 0x3f) | 0x80;
}
*p++ = ((i >> 18) & 0x3f) | 0x80;
}
*p++ = ((i >> 12) & 0x3f) | 0x80;
}
*p++ = ((i >> 6) & 0x3f) | 0x80;
}
*p++ = (0x3f & i) | 0x80;
}
*p = '\0';
return (p - buf);
}
static void
bench_char_type(gpointer user_data)
{
uint64_t code_point;
char utf8[7];
for (code_point = 1; code_point < MAX_UNICODE; code_point++) {
ucs2utf8(code_point, (unsigned char *)utf8);
grn_nfkc50_char_type(utf8);
}
}
static void
bench_decompose(gpointer user_data)
{
uint64_t code_point;
char utf8[7];
for (code_point = 1; code_point < MAX_UNICODE; code_point++) {
ucs2utf8(code_point, (unsigned char *)utf8);
grn_nfkc50_decompose(utf8);
}
}
static void
bench_compose_no_change(gpointer user_data)
{
uint64_t prefix_code_point;
uint64_t suffix_code_point = 0x61; /* a */
char prefix_utf8[7];
char suffix_utf8[7];
ucs2utf8(suffix_code_point, (unsigned char *)suffix_utf8);
for (prefix_code_point = 1;
prefix_code_point < MAX_UNICODE;
prefix_code_point++) {
ucs2utf8(prefix_code_point, (unsigned char *)prefix_utf8);
grn_nfkc50_compose(prefix_utf8, suffix_utf8);
}
}
static void
bench_compose_change(gpointer user_data)
{
uint64_t prefix_code_point;
uint64_t suffix_code_point = 0x11ba;
char prefix_utf8[7];
char suffix_utf8[7];
ucs2utf8(suffix_code_point, (unsigned char *)suffix_utf8);
for (prefix_code_point = 1;
prefix_code_point < MAX_UNICODE;
prefix_code_point++) {
ucs2utf8(prefix_code_point, (unsigned char *)prefix_utf8);
grn_nfkc50_compose(prefix_utf8, suffix_utf8);
}
}
/*
static void
check_char_type(gpointer user_data)
{
uint64_t code_point;
char utf8[7];
for (code_point = 1; code_point < MAX_UNICODE; code_point++) {
grn_char_type a;
grn_char_type b;
ucs2utf8(code_point, (unsigned char *)utf8);
a = grn_nfkc_char_type(utf8);
b = grn_nfkc50_char_type(utf8);
if (a == b) {
continue;
}
printf("%lx: %s: %d != %d\n", code_point, utf8, a, b);
}
}
static void
check_decompose(gpointer user_data)
{
uint64_t code_point;
char utf8[7];
for (code_point = 1; code_point < MAX_UNICODE; code_point++) {
const char *a;
const char *b;
ucs2utf8(code_point, (unsigned char *)utf8);
a = grn_nfkc_decompose(utf8);
b = grn_nfkc50_decompose(utf8);
if (a == b) {
continue;
}
if (!a || !b) {
printf("%lx: %s: %s != %s\n", code_point, utf8, a, b);
continue;
}
if (strcmp(a, b) != 0) {
printf("%lx: %s: %s != %s\n", code_point, utf8, a, b);
}
}
}
static void
check_compose(gpointer user_data)
{
uint64_t prefix_code_point;
uint64_t suffix_code_point;
char prefix_utf8[7];
char suffix_utf8[7];
for (prefix_code_point = 1;
prefix_code_point < MAX_UNICODE;
prefix_code_point++) {
ucs2utf8(prefix_code_point, (unsigned char *)prefix_utf8);
for (suffix_code_point = 1;
suffix_code_point < MAX_UNICODE;
suffix_code_point++) {
const char *a;
const char *b;
ucs2utf8(suffix_code_point, (unsigned char *)suffix_utf8);
a = grn_nfkc_compose(prefix_utf8, suffix_utf8);
b = grn_nfkc50_compose(prefix_utf8, suffix_utf8);
if (a == b) {
continue;
}
if (!a || !b) {
printf("%lx-%lx: %s-%s: %s != %s\n",
prefix_code_point, suffix_code_point,
prefix_utf8, suffix_utf8,
a, b);
continue;
}
if (strcmp(a, b) != 0) {
printf("%lx-%lx: %s-%s: %s != %s\n",
prefix_code_point, suffix_code_point,
prefix_utf8, suffix_utf8,
a, b);
}
}
if ((prefix_code_point % 10000) == 0) {
printf("%" G_GUINT64_FORMAT "\n", prefix_code_point);
}
}
}
*/
int
main(int argc, gchar **argv)
{
grn_rc rc;
BenchReporter *reporter;
gint n = 10;
rc = grn_init();
if (rc != GRN_SUCCESS) {
g_print("failed to initialize Groonga: <%d>: %s\n",
rc, grn_get_global_error_message());
return EXIT_FAILURE;
}
bench_init(&argc, &argv);
reporter = bench_reporter_new();
if (g_getenv("N")) {
n = atoi(g_getenv("N"));
}
#define REGISTER(label, bench_function) \
bench_reporter_register(reporter, label, n, \
NULL, \
bench_function, \
NULL, \
NULL)
REGISTER("char_type ", bench_char_type);
REGISTER("decompose ", bench_decompose);
REGISTER("compose - no change", bench_compose_no_change);
REGISTER("compose - change", bench_compose_change);
/*
REGISTER("check - char_type", check_char_type);
REGISTER("check - decompose", check_decompose);
REGISTER("check - compose ", check_compose);
*/
#undef REGISTER
bench_reporter_run(reporter);
g_object_unref(reporter);
return EXIT_SUCCESS;
}
|