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
|
// =================================================================================================
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include <stdexcept>
//#define ENABLE_XMP_CPP_INTERFACE 1;
using namespace std;
#if WIN_ENV
#pragma warning ( disable : 4701 ) // local variable may be used without having been initialized
#endif
// =================================================================================================
#include "public/include/XMP_Environment.h"
#include "public/include/XMP_Const.h"
#include "source/EndianUtils.hpp"
#include "source/UnicodeConversions.hpp"
#include "source/UnicodeConversions.cpp"
#define TestUnicodeConsortium 0
#if TestUnicodeConsortium
#include "ConvertUTF.c" // The Unicode Consortium implementations.
#endif
// =================================================================================================
#define kCodePointCount 0x110000
UTF8Unit sU8 [kCodePointCount*4 + 8];
UTF16Unit sU16 [kCodePointCount*2 + 4];
UTF32Unit sU32 [kCodePointCount + 2];
// =================================================================================================
static UTF8_to_UTF16_Proc OurUTF8_to_UTF16; // ! Don't use static initialization, VS.Net strips it!
static UTF8_to_UTF32_Proc OurUTF8_to_UTF32;
static UTF16_to_UTF8_Proc OurUTF16_to_UTF8;
static UTF16_to_UTF32_Proc OurUTF16_to_UTF32;
static UTF32_to_UTF8_Proc OurUTF32_to_UTF8;
static UTF32_to_UTF16_Proc OurUTF32_to_UTF16;
// =================================================================================================
static void ReportPerformance ( FILE * log, const char * content, const size_t u32Count, const size_t u16Count, const size_t u8Count )
{
size_t inCount, outCount;
UTF32Unit * u32Ptr;
UTF16Unit * u16Ptr;
UTF8Unit * u8Ptr;
size_t i;
const size_t cycles = 100;
clock_t start, end;
double elapsed;
// --------------------------------------------------
fprintf ( log, "\n Adobe code over %s\n", content );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF32_to_UTF8 ( sU32, u32Count, sU8, sizeof(sU8), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF32_to_UTF8 : %.3f seconds\n", elapsed );
if ( (inCount != u32Count) || (outCount != u8Count) ) fprintf ( log, " *** Our UTF32_to_UTF8 count error, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF32_to_UTF16 ( sU32, u32Count, sU16, sizeof(sU16), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF32_to_UTF16 : %.3f seconds\n", elapsed );
if ( (inCount != u32Count) || (outCount != u16Count) ) fprintf ( log, " *** Our UTF32_to_UTF16 count error, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF16_to_UTF8 ( sU16, u16Count, sU8, sizeof(sU8), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF16_to_UTF8 : %.3f seconds\n", elapsed );
if ( (inCount != u16Count) || (outCount != u8Count) ) fprintf ( log, " *** Our UTF16_to_UTF8 count error, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF16_to_UTF32 ( sU16, u16Count, sU32, sizeof(sU32), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF16_to_UTF32 : %.3f seconds\n", elapsed );
if ( (inCount != u16Count) || (outCount != u32Count) ) fprintf ( log, " *** Our UTF16_to_UTF32 count error, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF8_to_UTF16 ( sU8, u8Count, sU16, sizeof(sU16), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF8_to_UTF16 : %.3f seconds\n", elapsed );
if ( (inCount != u8Count) || (outCount != u16Count) ) fprintf ( log, " *** Our UTF8_to_UTF16 count error, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) OurUTF8_to_UTF32 ( sU8, u8Count, sU32, sizeof(sU32), &inCount, &outCount );
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF8_to_UTF32 : %.3f seconds\n", elapsed );
if ( (inCount != u8Count) || (outCount != u32Count) ) fprintf ( log, " *** Our UTF8_to_UTF32 count error, %d -> %d\n", inCount, outCount );
#if TestUnicodeConsortium
// ---------------------------------------------------------------
fprintf ( log, "\n Unicode Consortium code over %s\n", content );
ConversionResult ucStatus;
start = clock();
for ( i = 0; i < cycles; ++i ) {
u32Ptr = sU32; u8Ptr = sU8;
ucStatus = ConvertUTF32toUTF8 ( (const UTF32**)(&u32Ptr), (const UTF32*)(sU32+u32Count), &u8Ptr, sU8+sizeof(sU8), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF32_to_UTF8 : %.3f seconds\n", elapsed );
inCount = u32Ptr - sU32; outCount = u8Ptr - sU8;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF32toUTF8 status error, %d\n", ucStatus );
if ( (inCount != u32Count) || (outCount != u8Count) ) fprintf ( log, " *** UC ConvertUTF32toUTF8 count error, %d, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) {
u32Ptr = sU32; u16Ptr = sU16;
ucStatus = ConvertUTF32toUTF16 ( (const UTF32**)(&u32Ptr), (const UTF32*)(sU32+u32Count), &u16Ptr, sU16+sizeof(sU16), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF32_to_UTF16 : %.3f seconds\n", elapsed );
inCount = u32Ptr - sU32; outCount = u16Ptr - sU16;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF32toUTF16 status error, %d\n", ucStatus );
if ( (inCount != u32Count) || (outCount != u16Count) ) fprintf ( log, " *** UC ConvertUTF32toUTF16 count error, %d, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) {
u16Ptr = sU16; u8Ptr = sU8;
ucStatus = ConvertUTF16toUTF8 ( (const UTF16**)(&u16Ptr), (const UTF16*)(sU16+u16Count), &u8Ptr, sU8+sizeof(sU8), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF16_to_UTF8 : %.3f seconds\n", elapsed );
inCount = u16Ptr - sU16; outCount = u8Ptr - sU8;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF16toUTF8 status error, %d\n", ucStatus );
if ( (inCount != u16Count) || (outCount != u8Count) ) fprintf ( log, " *** UC ConvertUTF16toUTF8 count error, %d, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) {
u16Ptr = sU16; u32Ptr = sU32;
ucStatus = ConvertUTF16toUTF32 ( (const UTF16**)(&u16Ptr), (const UTF16*)(sU16+u16Count), &u32Ptr, sU32+sizeof(sU32), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF16_to_UTF32 : %.3f seconds\n", elapsed );
inCount = u16Ptr - sU16; outCount = u32Ptr - sU32;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF16toUTF32 status error, %d\n", ucStatus );
if ( (inCount != u16Count) || (outCount != u32Count) ) fprintf ( log, " *** UC ConvertUTF16toUTF32 count error, %d, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) {
u8Ptr = sU8; u16Ptr = sU16;
ucStatus = ConvertUTF8toUTF16 ( (const UTF8**)(&u8Ptr), (const UTF8*)(sU8+u8Count), &u16Ptr, sU16+sizeof(sU16), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF8_to_UTF16 : %.3f seconds\n", elapsed );
inCount = u8Ptr - sU8; outCount = u16Ptr - sU16;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF8toUTF16 status error, %d\n", ucStatus );
if ( (inCount != u8Count) || (outCount != u16Count) ) fprintf ( log, " *** UC ConvertUTF8toUTF16 count error, %d, %d -> %d\n", inCount, outCount );
start = clock();
for ( i = 0; i < cycles; ++i ) {
u8Ptr = sU8; u32Ptr = sU32;
ucStatus = ConvertUTF8toUTF32 ( (const UTF8**)(&u8Ptr), (const UTF8*)(sU8+u8Count), &u32Ptr, sU32+sizeof(sU32), strictConversion );
}
end = clock();
elapsed = double(end-start) / CLOCKS_PER_SEC;
fprintf ( log, " UTF8_to_UTF32 : %.3f seconds\n", elapsed );
inCount = u8Ptr - sU8; outCount = u32Ptr - sU32;
if ( ucStatus != conversionOK ) fprintf ( log, " *** UC ConvertUTF8toUTF32 status error, %d\n", ucStatus );
if ( (inCount != u8Count) || (outCount != u32Count) ) fprintf ( log, " *** UC ConvertUTF8toUTF32 count error, %d, %d -> %d\n", inCount, outCount );
#endif
} // ReportPerformance
// =================================================================================================
static void ComparePerformance ( FILE * log )
{
size_t i, u32Count, u16Count, u8Count;
UTF32Unit cp;
if ( kBigEndianHost ) {
OurUTF8_to_UTF16 = UTF8_to_UTF16BE;
OurUTF8_to_UTF32 = UTF8_to_UTF32BE;
OurUTF16_to_UTF8 = UTF16BE_to_UTF8;
OurUTF16_to_UTF32 = UTF16BE_to_UTF32BE;
OurUTF32_to_UTF8 = UTF32BE_to_UTF8;
OurUTF32_to_UTF16 = UTF32BE_to_UTF16BE;
} else {
OurUTF8_to_UTF16 = UTF8_to_UTF16LE;
OurUTF8_to_UTF32 = UTF8_to_UTF32LE;
OurUTF16_to_UTF8 = UTF16LE_to_UTF8;
OurUTF16_to_UTF32 = UTF16LE_to_UTF32LE;
OurUTF32_to_UTF8 = UTF32LE_to_UTF8;
OurUTF32_to_UTF16 = UTF32LE_to_UTF16LE;
}
for ( i = 0, cp = 0; cp < 0xD800; ++i, ++cp ) sU32[i] = cp; // Measure using the full Unicode set.
for ( cp = 0xE000; cp < 0x110000; ++i, ++cp ) sU32[i] = cp;
u32Count = 0xD800 + (0x110000 - 0xE000);
u16Count = 0xD800 + (0x10000 - 0xE000) + (0x110000 - 0x10000)*2;
u8Count = 0x80 + (0x800 - 0x80)*2 + (0xD800 - 0x800)*3 + (0x10000 - 0xE000)*3 + (0x110000 - 0x10000)*4;
ReportPerformance ( log, "full Unicode set", u32Count, u16Count, u8Count );
for ( i = 0; i < 0x110000; ++i ) sU32[i] = i & 0x7F; // Measure using just ASCII.
u32Count = 0x110000;
u16Count = 0x110000;
u8Count = 0x110000;
ReportPerformance ( log, "just ASCII", u32Count, u16Count, u8Count );
for ( i = 0; i < 0x110000; ++i ) sU32[i] = 0x4000 + (i & 0x7FFF); // Measure using just non-ASCII inside the BMP.
u32Count = 0x110000;
u16Count = 0x110000;
u8Count = 0x110000*3;
ReportPerformance ( log, "just non-ASCII inside the BMP", u32Count, u16Count, u8Count );
for ( i = 0; i < 0x110000; ++i ) sU32[i] = 0x40000 + (i & 0xFFFF); // Measure using just outside the BMP.
u32Count = 0x110000;
u16Count = 0x110000*2;
u8Count = 0x110000*4;
ReportPerformance ( log, "just outside the BMP", u32Count, u16Count, u8Count );
} // ComparePerformance
// =================================================================================================
static void DoTest ( FILE * log )
{
InitializeUnicodeConversions();
ComparePerformance ( log );
} // DoTest
// =================================================================================================
extern "C" int main ( void )
{
char buffer [1000];
#if !XMP_AutomatedTestBuild
FILE * log = stdout;
#else
FILE * log = fopen ( "TestUnicode.out", "wb" );
#endif
time_t now;
time ( &now );
sprintf ( buffer, "// Starting test for Unicode conversion performance, %s", ctime ( &now ) );
fprintf ( log, "// " );
for ( size_t i = 4; i < strlen(buffer); ++i ) fprintf ( log, "=" );
fprintf ( log, "\n%s", buffer );
fprintf ( log, "// Native %s endian\n", (kBigEndianHost ? "big" : "little") );
try {
DoTest ( log );
} catch ( ... ) {
fprintf ( log, "\n## Caught unexpected exception\n" );
return -1;
}
time ( &now );
sprintf ( buffer, "// Finished test for Unicode conversion performance, %s", ctime ( &now ) );
fprintf ( log, "\n// " );
for ( size_t i = 4; i < strlen(buffer); ++i ) fprintf ( log, "=" );
fprintf ( log, "\n%s\n", buffer );
fclose ( log );
return 0;
}
|