1 /***********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 ***********************************************************************
5 ***********************************************************************
7 * Copyright (C) 2001-2012 IBM, Inc. All Rights Reserved.
9 ***********************************************************************/
10 /********************************************************************************
14 * Modification History:
16 * Andy Heninger First Version
18 *********************************************************************************
22 // This program tests string collation and sort key generation performance.
23 // Three APIs can be teste: ICU C , Unix strcoll, strxfrm and Windows LCMapString
24 // A file of names is required as input, one per line. It must be in utf-8 or utf-16 format,
25 // and include a byte order mark. Either LE or BE format is OK.
28 const char gUsageString
[] =
29 "usage: collperf options...\n"
30 "-help Display this message.\n"
31 "-file file_name utf-16 format file of names.\n"
32 "-locale name ICU locale to use. Default is en_US\n"
33 "-rules file_name Collation rules file (overrides locale)\n"
34 "-langid 0x1234 Windows Language ID number. Default to value for -locale option\n"
35 " see http://msdn.microsoft.com/library/psdk/winbase/nls_8xo3.htm\n"
36 "-win Run test using Windows native services. (ICU is default)\n"
37 "-unix Run test using Unix strxfrm, strcoll services.\n"
38 "-uselen Use API with string lengths. Default is null-terminated strings\n"
39 "-usekeys Run tests using sortkeys rather than strcoll\n"
40 "-strcmp Run tests using u_strcmp rather than strcoll\n"
41 "-strcmpCPO Run tests using u_strcmpCodePointOrder rather than strcoll\n"
42 "-loop nnnn Loopcount for test. Adjust for reasonable total running time.\n"
43 "-iloop n Inner Loop Count. Default = 1. Number of calls to function\n"
44 " under test at each call point. For measuring test overhead.\n"
45 "-terse Terse numbers-only output. Intended for use by scripts.\n"
46 "-french French accent ordering\n"
47 "-frenchoff No French accent ordering (for use with French locales.)\n"
48 "-norm Normalizing mode on\n"
49 "-shifted Shifted mode\n"
50 "-lower Lower case first\n"
51 "-upper Upper case first\n"
52 "-case Enable separate case level\n"
53 "-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
54 "-keyhist Produce a table sort key size vs. string length\n"
55 "-binsearch Binary Search timing test\n"
56 "-keygen Sort Key Generation timing test\n"
57 "-qsort Quicksort timing test\n"
58 "-iter Iteration Performance Test\n"
59 "-dump Display strings, sort keys and CEs.\n"
71 #include <unicode/utypes.h>
72 #include <unicode/ucol.h>
73 #include <unicode/ucoleitr.h>
74 #include <unicode/uloc.h>
75 #include <unicode/ustring.h>
76 #include <unicode/ures.h>
77 #include <unicode/uchar.h>
78 #include <unicode/ucnv.h>
79 #include <unicode/utf8.h>
85 // Stubs for Windows API functions when building on UNIXes.
88 inline int CompareStringW(DWORD
, DWORD
, UChar
*, int, UChar
*, int) {return 0;}
90 unsigned long timeGetTime() {
93 unsigned long val
= t
.tv_sec
* 1000; // Let it overflow. Who cares.
94 val
+= t
.tv_usec
/ 1000;
97 inline int LCMapStringW(DWORD
, DWORD
, UChar
*, int, UChar
*, int) {return 0;}
98 const int LCMAP_SORTKEY
= 0;
99 #define MAKELCID(a,b) 0
100 const int SORT_DEFAULT
= 0;
106 // Command line option variables
107 // These global variables are set according to the options specified
108 // on the command line by the user.
109 char * opt_fName
= 0;
110 const char * opt_locale
= "en_US";
111 int opt_langid
= 0; // Defaults to value corresponding to opt_locale.
112 char * opt_rules
= 0;
113 UBool opt_help
= FALSE
;
114 int opt_loopCount
= 1;
115 int opt_iLoopCount
= 1;
116 UBool opt_terse
= FALSE
;
117 UBool opt_qsort
= FALSE
;
118 UBool opt_binsearch
= FALSE
;
119 UBool opt_icu
= TRUE
;
120 UBool opt_win
= FALSE
; // Run with Windows native functions.
121 UBool opt_unix
= FALSE
; // Run with UNIX strcoll, strxfrm functions.
122 UBool opt_uselen
= FALSE
;
123 UBool opt_usekeys
= FALSE
;
124 UBool opt_strcmp
= FALSE
;
125 UBool opt_strcmpCPO
= FALSE
;
126 UBool opt_norm
= FALSE
;
127 UBool opt_keygen
= FALSE
;
128 UBool opt_french
= FALSE
;
129 UBool opt_frenchoff
= FALSE
;
130 UBool opt_shifted
= FALSE
;
131 UBool opt_lower
= FALSE
;
132 UBool opt_upper
= FALSE
;
133 UBool opt_case
= FALSE
;
135 UBool opt_keyhist
= FALSE
;
136 UBool opt_itertest
= FALSE
;
137 UBool opt_dump
= FALSE
;
142 // Definitions for the command line options
146 enum {FLAG
, NUM
, STRING
} type
;
151 {"-file", OptSpec::STRING
, &opt_fName
},
152 {"-locale", OptSpec::STRING
, &opt_locale
},
153 {"-langid", OptSpec::NUM
, &opt_langid
},
154 {"-rules", OptSpec::STRING
, &opt_rules
},
155 {"-qsort", OptSpec::FLAG
, &opt_qsort
},
156 {"-binsearch", OptSpec::FLAG
, &opt_binsearch
},
157 {"-iter", OptSpec::FLAG
, &opt_itertest
},
158 {"-win", OptSpec::FLAG
, &opt_win
},
159 {"-unix", OptSpec::FLAG
, &opt_unix
},
160 {"-uselen", OptSpec::FLAG
, &opt_uselen
},
161 {"-usekeys", OptSpec::FLAG
, &opt_usekeys
},
162 {"-strcmp", OptSpec::FLAG
, &opt_strcmp
},
163 {"-strcmpCPO", OptSpec::FLAG
, &opt_strcmpCPO
},
164 {"-norm", OptSpec::FLAG
, &opt_norm
},
165 {"-french", OptSpec::FLAG
, &opt_french
},
166 {"-frenchoff", OptSpec::FLAG
, &opt_frenchoff
},
167 {"-shifted", OptSpec::FLAG
, &opt_shifted
},
168 {"-lower", OptSpec::FLAG
, &opt_lower
},
169 {"-upper", OptSpec::FLAG
, &opt_upper
},
170 {"-case", OptSpec::FLAG
, &opt_case
},
171 {"-level", OptSpec::NUM
, &opt_level
},
172 {"-keyhist", OptSpec::FLAG
, &opt_keyhist
},
173 {"-keygen", OptSpec::FLAG
, &opt_keygen
},
174 {"-loop", OptSpec::NUM
, &opt_loopCount
},
175 {"-iloop", OptSpec::NUM
, &opt_iLoopCount
},
176 {"-terse", OptSpec::FLAG
, &opt_terse
},
177 {"-dump", OptSpec::FLAG
, &opt_dump
},
178 {"-help", OptSpec::FLAG
, &opt_help
},
179 {"-?", OptSpec::FLAG
, &opt_help
},
180 {0, OptSpec::FLAG
, 0}
184 //---------------------------------------------------------------------------
186 // Global variables pointing to and describing the test file
188 //---------------------------------------------------------------------------
193 // Each line from the source file (containing a name, presumably) gets
194 // one of these structs.
207 Line
*gFileLines
; // Ptr to array of Line structs, one per line in the file.
218 //---------------------------------------------------------------------------
220 // ProcessOptions() Function to read the command line options.
222 //---------------------------------------------------------------------------
223 UBool
ProcessOptions(int argc
, const char **argv
, OptSpec opts
[])
227 const char *pArgName
;
230 for (argNum
=1; argNum
<argc
; argNum
++) {
231 pArgName
= argv
[argNum
];
232 for (pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
233 if (strcmp(pOpt
->name
, pArgName
) == 0) {
234 switch (pOpt
->type
) {
236 *(UBool
*)(pOpt
->pVar
) = TRUE
;
238 case OptSpec::STRING
:
240 if (argNum
>= argc
) {
241 fprintf(stderr
, "value expected for \"%s\" option.\n", pOpt
->name
);
244 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
248 if (argNum
>= argc
) {
249 fprintf(stderr
, "value expected for \"%s\" option.\n", pOpt
->name
);
253 i
= strtol(argv
[argNum
], &endp
, 0);
254 if (endp
== argv
[argNum
]) {
255 fprintf(stderr
, "integer value expected for \"%s\" option.\n", pOpt
->name
);
258 *(int *)(pOpt
->pVar
) = i
;
265 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
272 //---------------------------------------------------------------------------------------
274 // Comparison functions for use by qsort.
276 // Six flavors, ICU or Windows, SortKey or String Compare, Strings with length
277 // or null terminated.
279 //---------------------------------------------------------------------------------------
280 int ICUstrcmpK(const void *a
, const void *b
) {
282 int t
= strcmp((*(Line
**)a
)->icuSortKey
, (*(Line
**)b
)->icuSortKey
);
287 int ICUstrcmpL(const void *a
, const void *b
) {
290 t
= ucol_strcoll(gCol
, (*(Line
**)a
)->name
, (*(Line
**)a
)->len
, (*(Line
**)b
)->name
, (*(Line
**)b
)->len
);
291 if (t
== UCOL_LESS
) return -1;
292 if (t
== UCOL_GREATER
) return +1;
297 int ICUstrcmp(const void *a
, const void *b
) {
300 t
= ucol_strcoll(gCol
, (*(Line
**)a
)->name
, -1, (*(Line
**)b
)->name
, -1);
301 if (t
== UCOL_LESS
) return -1;
302 if (t
== UCOL_GREATER
) return +1;
307 int Winstrcmp(const void *a
, const void *b
) {
310 t
= CompareStringW(gWinLCID
, 0, (*(Line
**)a
)->name
, -1, (*(Line
**)b
)->name
, -1);
315 int UNIXstrcmp(const void *a
, const void *b
) {
318 t
= strcoll((*(Line
**)a
)->unixName
, (*(Line
**)b
)->unixName
);
323 int WinstrcmpL(const void *a
, const void *b
) {
326 t
= CompareStringW(gWinLCID
, 0, (*(Line
**)a
)->name
, (*(Line
**)a
)->len
, (*(Line
**)b
)->name
, (*(Line
**)b
)->len
);
331 int WinstrcmpK(const void *a
, const void *b
) {
333 int t
= strcmp((*(Line
**)a
)->winSortKey
, (*(Line
**)b
)->winSortKey
);
338 //---------------------------------------------------------------------------------------
340 // Function for sorting the names (lines) into a random order.
341 // Order is based on a hash of the ICU Sort key for the lines
342 // The randomized order is used as input for the sorting timing tests.
344 //---------------------------------------------------------------------------------------
345 int ICURandomCmp(const void *a
, const void *b
) {
346 char *ask
= (*(Line
**)a
)->icuSortKey
;
347 char *bsk
= (*(Line
**)b
)->icuSortKey
;
352 aVal
+= aVal
*37 + *ask
++;
355 bVal
+= bVal
*37 + *bsk
++;
361 else if (aVal
> bVal
) {
367 //---------------------------------------------------------------------------------------
369 // doKeyGen() Key Generation Timing Test
371 //---------------------------------------------------------------------------------------
380 // Adjust loop count to compensate for file size. Should be order n
381 double dLoopCount
= double(opt_loopCount
) * (1000. / double(gNumFileLines
));
382 int adj_loopCount
= int(dLoopCount
);
383 if (adj_loopCount
< 1) adj_loopCount
= 1;
386 unsigned long startTime
= timeGetTime();
389 for (loops
=0; loops
<adj_loopCount
; loops
++) {
390 for (line
=0; line
< gNumFileLines
; line
++) {
392 len
= gFileLines
[line
].len
;
394 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
395 t
=LCMapStringW(gWinLCID
, LCMAP_SORTKEY
,
396 gFileLines
[line
].name
, len
,
397 (unsigned short *)gFileLines
[line
].winSortKey
, 5000); // TODO something with length.
404 for (loops
=0; loops
<adj_loopCount
; loops
++) {
405 for (line
=0; line
< gNumFileLines
; line
++) {
407 len
= gFileLines
[line
].len
;
409 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
410 t
= ucol_getSortKey(gCol
, gFileLines
[line
].name
, len
, (unsigned char *)gFileLines
[line
].icuSortKey
, 5000);
417 for (loops
=0; loops
<adj_loopCount
; loops
++) {
418 for (line
=0; line
< gNumFileLines
; line
++) {
419 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
420 t
= strxfrm(gFileLines
[line
].unixSortKey
, gFileLines
[line
].unixName
, 5000);
426 unsigned long elapsedTime
= timeGetTime() - startTime
;
427 int ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)(adj_loopCount
*gNumFileLines
));
429 if (opt_terse
== FALSE
) {
430 printf("Sort Key Generation: total # of keys = %d\n", loops
*gNumFileLines
);
431 printf("Sort Key Generation: time per key = %d ns\n", ns
);
439 for (line
=0; line
<gNumFileLines
; line
++) {
440 totalChars
+= u_strlen(gFileLines
[line
].name
);
442 totalKeyLen
+= strlen(gFileLines
[line
].winSortKey
);
445 totalKeyLen
+= strlen(gFileLines
[line
].icuSortKey
);
448 totalKeyLen
+= strlen(gFileLines
[line
].unixSortKey
);
452 if (opt_terse
== FALSE
) {
453 printf("Key Length / character = %f\n", (float)totalKeyLen
/ (float)totalChars
);
455 printf("%f, ", (float)totalKeyLen
/ (float)totalChars
);
461 //---------------------------------------------------------------------------------------
463 // doBinarySearch() Binary Search timing test. Each name from the list
464 // is looked up in the full sorted list of names.
466 //---------------------------------------------------------------------------------------
467 void doBinarySearch()
474 unsigned long elapsedTime
= 0;
476 // Adjust loop count to compensate for file size. Should be order n (lookups) * log n (compares/lookup)
477 // Accurate timings do not depend on this being perfect. The correction is just to try to
478 // get total running times of about the right order, so the that user doesn't need to
479 // manually adjust the loop count for every different file size.
480 double dLoopCount
= double(opt_loopCount
) * 3000. / (log10((double)gNumFileLines
) * double(gNumFileLines
));
481 if (opt_usekeys
) dLoopCount
*= 5;
482 int adj_loopCount
= int(dLoopCount
);
483 if (adj_loopCount
< 1) adj_loopCount
= 1;
486 for (;;) { // not really a loop, just allows "break" to work, to simplify
487 // inadvertantly running more than one test through here.
488 if (opt_strcmp
|| opt_strcmpCPO
)
490 unsigned long startTime
= timeGetTime();
491 typedef int32_t (U_EXPORT2
*PF
)(const UChar
*, const UChar
*);
493 if (opt_strcmpCPO
) {pf
= u_strcmpCodePointOrder
;}
494 //if (opt_strcmp && opt_win) {pf = (PF)wcscmp;} // Damn the difference between int32_t and int
495 // which forces the use of a cast here.
498 for (loops
=0; loops
<adj_loopCount
; loops
++) {
500 for (line
=0; line
< gNumFileLines
; line
++) {
501 int hi
= gNumFileLines
-1;
505 int newGuess
= (hi
+ lo
) / 2;
506 if (newGuess
== guess
)
509 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
510 r
= (*pf
)((gSortedLines
[line
])->name
, (gSortedLines
[guess
])->name
);
522 elapsedTime
= timeGetTime() - startTime
;
529 unsigned long startTime
= timeGetTime();
530 UCollationResult r
= UCOL_EQUAL
;
531 for (loops
=0; loops
<adj_loopCount
; loops
++) {
533 for (line
=0; line
< gNumFileLines
; line
++) {
537 lineLen
= (gSortedLines
[line
])->len
;
539 int hi
= gNumFileLines
-1;
543 int newGuess
= (hi
+ lo
) / 2;
544 if (newGuess
== guess
)
549 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
550 ri
= strcmp((gSortedLines
[line
])->icuSortKey
, (gSortedLines
[guess
])->icuSortKey
);
553 r
=UCOL_GREATER
; if(ri
<0) {r
=UCOL_LESS
;} else if (ri
==0) {r
=UCOL_EQUAL
;}
558 guessLen
= (gSortedLines
[guess
])->len
;
560 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
561 r
= ucol_strcoll(gCol
, (gSortedLines
[line
])->name
, lineLen
, (gSortedLines
[guess
])->name
, guessLen
);
574 elapsedTime
= timeGetTime() - startTime
;
580 unsigned long startTime
= timeGetTime();
582 for (loops
=0; loops
<adj_loopCount
; loops
++) {
584 for (line
=0; line
< gNumFileLines
; line
++) {
588 lineLen
= (gSortedLines
[line
])->len
;
590 int hi
= gNumFileLines
-1;
594 int newGuess
= (hi
+ lo
) / 2;
595 if (newGuess
== guess
)
599 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
600 r
= strcmp((gSortedLines
[line
])->winSortKey
, (gSortedLines
[guess
])->winSortKey
);
608 guessLen
= (gSortedLines
[guess
])->len
;
610 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
611 r
= CompareStringW(gWinLCID
, 0, (gSortedLines
[line
])->name
, lineLen
, (gSortedLines
[guess
])->name
, guessLen
);
614 if (opt_terse
== FALSE
) {
615 fprintf(stderr
, "Error returned from Windows CompareStringW.\n");
621 if (r
== 2) // strings ==
623 if (r
== 1) // line < guess
630 elapsedTime
= timeGetTime() - startTime
;
636 unsigned long startTime
= timeGetTime();
638 for (loops
=0; loops
<adj_loopCount
; loops
++) {
640 for (line
=0; line
< gNumFileLines
; line
++) {
641 int hi
= gNumFileLines
-1;
645 int newGuess
= (hi
+ lo
) / 2;
646 if (newGuess
== guess
)
650 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
651 r
= strcmp((gSortedLines
[line
])->unixSortKey
, (gSortedLines
[guess
])->unixSortKey
);
657 for (iLoop
=0; iLoop
< opt_iLoopCount
; iLoop
++) {
658 r
= strcoll((gSortedLines
[line
])->unixName
, (gSortedLines
[guess
])->unixName
);
662 fprintf(stderr
, "Error %d returned from strcoll.\n", errno
);
667 if (r
== 0) // strings ==
669 if (r
< 0) // line < guess
676 elapsedTime
= timeGetTime() - startTime
;
682 int ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
683 if (opt_terse
== FALSE
) {
684 printf("binary search: total # of string compares = %d\n", gCount
);
685 printf("binary search: compares per loop = %d\n", gCount
/ loops
);
686 printf("binary search: time per compare = %d ns\n", ns
);
696 //---------------------------------------------------------------------------------------
698 // doQSort() The quick sort timing test. Uses the C library qsort function.
700 //---------------------------------------------------------------------------------------
703 Line
**sortBuf
= new Line
*[gNumFileLines
];
705 // Adjust loop count to compensate for file size. QSort should be n log(n)
706 double dLoopCount
= double(opt_loopCount
) * 3000. / (log10((double)gNumFileLines
) * double(gNumFileLines
));
707 if (opt_usekeys
) dLoopCount
*= 5;
708 int adj_loopCount
= int(dLoopCount
);
709 if (adj_loopCount
< 1) adj_loopCount
= 1;
713 unsigned long startTime
= timeGetTime();
714 if (opt_win
&& opt_usekeys
) {
715 for (i
=0; i
<opt_loopCount
; i
++) {
716 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
717 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), WinstrcmpK
);
721 else if (opt_win
&& opt_uselen
) {
722 for (i
=0; i
<adj_loopCount
; i
++) {
723 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
724 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), WinstrcmpL
);
729 else if (opt_win
&& !opt_uselen
) {
730 for (i
=0; i
<adj_loopCount
; i
++) {
731 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
732 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), Winstrcmp
);
736 else if (opt_icu
&& opt_usekeys
) {
737 for (i
=0; i
<adj_loopCount
; i
++) {
738 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
739 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), ICUstrcmpK
);
743 else if (opt_icu
&& opt_uselen
) {
744 for (i
=0; i
<adj_loopCount
; i
++) {
745 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
746 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), ICUstrcmpL
);
751 else if (opt_icu
&& !opt_uselen
) {
752 for (i
=0; i
<adj_loopCount
; i
++) {
753 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
754 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), ICUstrcmp
);
758 else if (opt_unix
&& !opt_usekeys
) {
759 for (i
=0; i
<adj_loopCount
; i
++) {
760 memcpy(sortBuf
, gRandomLines
, gNumFileLines
* sizeof(Line
*));
761 qsort(sortBuf
, gNumFileLines
, sizeof(Line
*), UNIXstrcmp
);
765 unsigned long elapsedTime
= timeGetTime() - startTime
;
766 int ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
767 if (opt_terse
== FALSE
) {
768 printf("qsort: total # of string compares = %d\n", gCount
);
769 printf("qsort: time per compare = %d ns\n", ns
);
777 //---------------------------------------------------------------------------------------
779 // doKeyHist() Output a table of data for
780 // average sort key size vs. string length.
782 //---------------------------------------------------------------------------------------
787 // Find the maximum string length
788 for (i
=0; i
<gNumFileLines
; i
++) {
789 if (gFileLines
[i
].len
> maxLen
) maxLen
= gFileLines
[i
].len
;
792 // Allocate arrays to hold the histogram data
793 int *accumulatedLen
= new int[maxLen
+1];
794 int *numKeysOfSize
= new int[maxLen
+1];
795 for (i
=0; i
<=maxLen
; i
++) {
796 accumulatedLen
[i
] = 0;
797 numKeysOfSize
[i
] = 0;
800 // Fill the arrays...
801 for (i
=0; i
<gNumFileLines
; i
++) {
802 int len
= gFileLines
[i
].len
;
803 accumulatedLen
[len
] += strlen(gFileLines
[i
].icuSortKey
);
804 numKeysOfSize
[len
] += 1;
807 // And write out averages
808 printf("String Length, Avg Key Length, Avg Key Len per char\n");
809 for (i
=1; i
<=maxLen
; i
++) {
810 if (numKeysOfSize
[i
] > 0) {
811 printf("%d, %f, %f\n", i
, (float)accumulatedLen
[i
] / (float)numKeysOfSize
[i
],
812 (float)accumulatedLen
[i
] / (float)(numKeysOfSize
[i
] * i
));
815 delete []accumulatedLen
;
816 delete []numKeysOfSize
;
819 //---------------------------------------------------------------------------------------
821 // doForwardIterTest(UBool) Forward iteration test
822 // argument null-terminated string used
824 //---------------------------------------------------------------------------------------
825 void doForwardIterTest(UBool haslen
) {
828 UErrorCode error
= U_ZERO_ERROR
;
829 printf("\n\nPerforming forward iteration performance test with ");
832 printf("non-null terminated data -----------\n");
835 printf("null terminated data -----------\n");
837 printf("performance test on strings from file -----------\n");
839 UChar dummytext
[] = {0, 0};
840 UCollationElements
*iter
= ucol_openElements(gCol
, NULL
, 0, &error
);
841 ucol_setText(iter
, dummytext
, 1, &error
);
844 unsigned long startTime
= timeGetTime();
845 while (count
< opt_loopCount
) {
847 while (linecount
< gNumFileLines
) {
848 UChar
*str
= gFileLines
[linecount
].name
;
849 int strlen
= haslen
?gFileLines
[linecount
].len
:-1;
850 ucol_setText(iter
, str
, strlen
, &error
);
851 while (ucol_next(iter
, &error
) != UCOL_NULLORDER
) {
859 unsigned long elapsedTime
= timeGetTime() - startTime
;
860 printf("elapsedTime %ld\n", elapsedTime
);
862 // empty loop recalculation
864 startTime
= timeGetTime();
865 while (count
< opt_loopCount
) {
867 while (linecount
< gNumFileLines
) {
868 UChar
*str
= gFileLines
[linecount
].name
;
869 int strlen
= haslen
?gFileLines
[linecount
].len
:-1;
870 ucol_setText(iter
, str
, strlen
, &error
);
875 elapsedTime
-= (timeGetTime() - startTime
);
876 printf("elapsedTime %ld\n", elapsedTime
);
878 ucol_closeElements(iter
);
880 int ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
881 printf("Total number of strings compared %d in %d loops\n", gNumFileLines
,
883 printf("Average time per ucol_next() nano seconds %d\n", ns
);
885 printf("performance test on skipped-5 concatenated strings from file -----------\n");
889 // appending all the strings
891 while (linecount
< gNumFileLines
) {
892 strlen
+= haslen
?gFileLines
[linecount
].len
:
893 u_strlen(gFileLines
[linecount
].name
);
896 str
= (UChar
*)malloc(sizeof(UChar
) * strlen
);
899 while (strindex
< strlen
) {
901 len
+= haslen
?gFileLines
[linecount
].len
:
902 u_strlen(gFileLines
[linecount
].name
);
903 memcpy(str
+ strindex
, gFileLines
[linecount
].name
,
904 sizeof(UChar
) * len
);
909 printf("Total size of strings %d\n", strlen
);
917 iter
= ucol_openElements(gCol
, str
, strlen
, &error
);
919 strlen
= u_strlen(str
);
921 strlen
-= 5; // any left over characters are not iterated,
922 // this is to ensure the backwards and forwards iterators
923 // gets the same position
924 startTime
= timeGetTime();
925 while (count
< opt_loopCount
) {
928 ucol_setOffset(iter
, strindex
, &error
);
930 if (ucol_next(iter
, &error
) == UCOL_NULLORDER
) {
937 if (strindex
> strlen
) {
940 ucol_setOffset(iter
, strindex
, &error
);
947 elapsedTime
= timeGetTime() - startTime
;
948 printf("elapsedTime %ld\n", elapsedTime
);
950 // empty loop recalculation
953 startTime
= timeGetTime();
954 while (count
< opt_loopCount
) {
957 ucol_setOffset(iter
, strindex
, &error
);
963 if (strindex
> strlen
) {
966 ucol_setOffset(iter
, strindex
, &error
);
972 elapsedTime
-= (timeGetTime() - startTime
);
973 printf("elapsedTime %ld\n", elapsedTime
);
975 ucol_closeElements(iter
);
977 printf("gCount %d\n", gCount
);
978 ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
979 printf("Average time per ucol_next() nano seconds %d\n", ns
);
982 //---------------------------------------------------------------------------------------
984 // doBackwardIterTest(UBool) Backwards iteration test
985 // argument null-terminated string used
987 //---------------------------------------------------------------------------------------
988 void doBackwardIterTest(UBool haslen
) {
990 UErrorCode error
= U_ZERO_ERROR
;
991 printf("\n\nPerforming backward iteration performance test with ");
994 printf("non-null terminated data -----------\n");
997 printf("null terminated data -----------\n");
1000 printf("performance test on strings from file -----------\n");
1002 UCollationElements
*iter
= ucol_openElements(gCol
, NULL
, 0, &error
);
1003 UChar dummytext
[] = {0, 0};
1004 ucol_setText(iter
, dummytext
, 1, &error
);
1007 unsigned long startTime
= timeGetTime();
1008 while (count
< opt_loopCount
) {
1010 while (linecount
< gNumFileLines
) {
1011 UChar
*str
= gFileLines
[linecount
].name
;
1012 int strlen
= haslen
?gFileLines
[linecount
].len
:-1;
1013 ucol_setText(iter
, str
, strlen
, &error
);
1014 while (ucol_previous(iter
, &error
) != UCOL_NULLORDER
) {
1022 unsigned long elapsedTime
= timeGetTime() - startTime
;
1024 printf("elapsedTime %ld\n", elapsedTime
);
1026 // empty loop recalculation
1028 startTime
= timeGetTime();
1029 while (count
< opt_loopCount
) {
1031 while (linecount
< gNumFileLines
) {
1032 UChar
*str
= gFileLines
[linecount
].name
;
1033 int strlen
= haslen
?gFileLines
[linecount
].len
:-1;
1034 ucol_setText(iter
, str
, strlen
, &error
);
1039 elapsedTime
-= (timeGetTime() - startTime
);
1041 printf("elapsedTime %ld\n", elapsedTime
);
1042 ucol_closeElements(iter
);
1044 int ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
1045 printf("Total number of strings compared %d in %d loops\n", gNumFileLines
,
1047 printf("Average time per ucol_previous() nano seconds %d\n", ns
);
1049 printf("performance test on skipped-5 concatenated strings from file -----------\n");
1053 // appending all the strings
1055 while (linecount
< gNumFileLines
) {
1056 strlen
+= haslen
?gFileLines
[linecount
].len
:
1057 u_strlen(gFileLines
[linecount
].name
);
1060 str
= (UChar
*)malloc(sizeof(UChar
) * strlen
);
1063 while (strindex
< strlen
) {
1065 len
+= haslen
?gFileLines
[linecount
].len
:
1066 u_strlen(gFileLines
[linecount
].name
);
1067 memcpy(str
+ strindex
, gFileLines
[linecount
].name
,
1068 sizeof(UChar
) * len
);
1073 printf("Total size of strings %d\n", strlen
);
1082 iter
= ucol_openElements(gCol
, str
, strlen
, &error
);
1084 strlen
= u_strlen(str
);
1087 startTime
= timeGetTime();
1088 while (count
< opt_loopCount
) {
1091 ucol_setOffset(iter
, strindex
, &error
);
1093 if (ucol_previous(iter
, &error
) == UCOL_NULLORDER
) {
1100 if (strindex
> strlen
) {
1103 ucol_setOffset(iter
, strindex
, &error
);
1110 elapsedTime
= timeGetTime() - startTime
;
1111 printf("elapsedTime %ld\n", elapsedTime
);
1113 // empty loop recalculation
1116 startTime
= timeGetTime();
1117 while (count
< opt_loopCount
) {
1120 ucol_setOffset(iter
, strindex
, &error
);
1126 if (strindex
> strlen
) {
1129 ucol_setOffset(iter
, strindex
, &error
);
1135 elapsedTime
-= (timeGetTime() - startTime
);
1136 printf("elapsedTime %ld\n", elapsedTime
);
1137 ucol_closeElements(iter
);
1139 printf("gCount %d\n", gCount
);
1140 ns
= (int)(float(1000000) * (float)elapsedTime
/ (float)gCount
);
1141 printf("Average time per ucol_previous() nano seconds %d\n", ns
);
1144 //---------------------------------------------------------------------------------------
1146 // doIterTest() Iteration test
1148 //---------------------------------------------------------------------------------------
1150 doForwardIterTest(opt_uselen
);
1151 doBackwardIterTest(opt_uselen
);
1155 //----------------------------------------------------------------------------------------
1157 // UnixConvert -- Convert the lines of the file to the encoding for UNIX
1158 // Since it appears that Unicode support is going in the general
1159 // direction of the use of UTF-8 locales, that is the approach
1160 // that is used here.
1162 //----------------------------------------------------------------------------------------
1163 void UnixConvert() {
1166 UConverter
*cvrtr
; // An ICU code page converter.
1167 UErrorCode status
= U_ZERO_ERROR
;
1170 cvrtr
= ucnv_open("utf-8", &status
); // we are just doing UTF-8 locales for now.
1171 if (U_FAILURE(status
)) {
1172 fprintf(stderr
, "ICU Converter open failed.: %s\n", u_errorName(status
));
1176 for (line
=0; line
< gNumFileLines
; line
++) {
1177 int sizeNeeded
= ucnv_fromUChars(cvrtr
,
1178 0, // ptr to target buffer.
1179 0, // length of target buffer.
1180 gFileLines
[line
].name
,
1181 -1, // source is null terminated
1183 if (status
!= U_BUFFER_OVERFLOW_ERROR
&& status
!= U_ZERO_ERROR
) {
1184 //fprintf(stderr, "Conversion from Unicode, something is wrong.\n");
1187 status
= U_ZERO_ERROR
;
1188 gFileLines
[line
].unixName
= new char[sizeNeeded
+1];
1189 sizeNeeded
= ucnv_fromUChars(cvrtr
,
1190 gFileLines
[line
].unixName
, // ptr to target buffer.
1191 sizeNeeded
+1, // length of target buffer.
1192 gFileLines
[line
].name
,
1193 -1, // source is null terminated
1195 if (U_FAILURE(status
)) {
1196 fprintf(stderr
, "ICU Conversion Failed.: %d\n", status
);
1199 gFileLines
[line
].unixName
[sizeNeeded
] = 0;
1205 //----------------------------------------------------------------------------------------
1207 // class UCharFile Class to hide all the gorp to read a file in
1208 // and produce a stream of UChars.
1210 //----------------------------------------------------------------------------------------
1213 UCharFile(const char *fileName
);
1216 UBool
eof() {return fEof
;};
1217 UBool
error() {return fError
;};
1220 UCharFile (const UCharFile
& /*other*/) {}; // No copy constructor.
1221 UCharFile
& operator = (const UCharFile
&/*other*/) {return *this;}; // No assignment op
1227 UChar fPending2ndSurrogate
;
1229 enum {UTF16LE
, UTF16BE
, UTF8
} fEncoding
;
1232 UCharFile::UCharFile(const char * fileName
) {
1236 fFile
= fopen(fName
, "rb");
1237 fPending2ndSurrogate
= 0;
1238 if (fFile
== NULL
) {
1239 fprintf(stderr
, "Can not open file \"%s\"\n", opt_fName
);
1244 // Look for the byte order mark at the start of the file.
1246 int BOMC1
, BOMC2
, BOMC3
;
1247 BOMC1
= fgetc(fFile
);
1248 BOMC2
= fgetc(fFile
);
1250 if (BOMC1
== 0xff && BOMC2
== 0xfe) {
1251 fEncoding
= UTF16LE
; }
1252 else if (BOMC1
== 0xfe && BOMC2
== 0xff) {
1253 fEncoding
= UTF16BE
; }
1254 else if (BOMC1
== 0xEF && BOMC2
== 0xBB && (BOMC3
= fgetc(fFile
)) == 0xBF ) {
1258 fprintf(stderr
, "collperf: file \"%s\" encoding must be UTF-8 or UTF-16, and "
1259 "must include a BOM.\n", fileName
);
1266 UCharFile::~UCharFile() {
1272 UChar
UCharFile::get() {
1274 switch (fEncoding
) {
1301 if (fPending2ndSurrogate
!= 0) {
1302 c
= fPending2ndSurrogate
;
1303 fPending2ndSurrogate
= 0;
1307 int ch
= fgetc(fFile
); // Note: c and ch are separate cause eof test doesn't work on UChar type.
1315 // It's ascii. No further utf-8 conversion.
1320 // Figure out the lenght of the char and read the rest of the bytes
1321 // into a temp array.
1323 if (ch
>= 0xF0) {nBytes
=4;}
1324 else if (ch
>= 0xE0) {nBytes
=3;}
1325 else if (ch
>= 0xC0) {nBytes
=2;}
1327 fprintf(stderr
, "utf-8 encoded file contains corrupt data.\n");
1332 unsigned char bytes
[10];
1333 bytes
[0] = (unsigned char)ch
;
1335 for (i
=1; i
<nBytes
; i
++) {
1336 bytes
[i
] = fgetc(fFile
);
1337 if (bytes
[i
] < 0x80 || bytes
[i
] >= 0xc0) {
1338 fprintf(stderr
, "utf-8 encoded file contains corrupt data.\n");
1344 // Convert the bytes from the temp array to a Unicode char.
1347 U8_NEXT_UNSAFE(bytes
, i
, cp
);
1350 if (cp
>= 0x10000) {
1351 // The code point needs to be broken up into a utf-16 surrogate pair.
1352 // Process first half this time through the main loop, and
1353 // remember the other half for the next time through.
1356 UTF16_APPEND_CHAR_UNSAFE(utf16Buf
, i
, cp
);
1357 fPending2ndSurrogate
= utf16Buf
[1];
1363 c
= 0xFFFD; /* Error, unspecified codepage*/
1364 fprintf(stderr
, "UCharFile: Error: unknown fEncoding\n");
1370 //----------------------------------------------------------------------------------------
1372 // openRulesCollator - Command line specified a rules file. Read it in
1373 // and open a collator with it.
1375 //----------------------------------------------------------------------------------------
1376 UCollator
*openRulesCollator() {
1377 UCharFile
f(opt_rules
);
1383 UChar
*buf
= (UChar
*)malloc(bufLen
* sizeof(UChar
));
1399 buf
= (UChar
*)realloc(buf
, bufLen
);
1408 UErrorCode status
= U_ZERO_ERROR
;
1409 UCollator
*coll
= ucol_openRules(buf
, u_strlen(buf
), UCOL_OFF
,
1410 UCOL_DEFAULT_STRENGTH
, NULL
, &status
);
1411 if (U_FAILURE(status
)) {
1412 fprintf(stderr
, "ICU ucol_openRules() open failed.: %d\n", status
);
1423 //----------------------------------------------------------------------------------------
1425 // Main -- process command line, read in and pre-process the test file,
1426 // call other functions to do the actual tests.
1428 //----------------------------------------------------------------------------------------
1429 int main(int argc
, const char** argv
) {
1430 if (ProcessOptions(argc
, argv
, opts
) != TRUE
|| opt_help
|| opt_fName
== 0) {
1431 printf(gUsageString
);
1435 // Make sure that we've only got one API selected.
1436 if (opt_unix
|| opt_win
) opt_icu
= FALSE
;
1437 if (opt_unix
) opt_win
= FALSE
;
1440 // Set up an ICU collator
1442 UErrorCode status
= U_ZERO_ERROR
;
1444 if (opt_rules
!= 0) {
1445 gCol
= openRulesCollator();
1446 if (gCol
== 0) {return -1;}
1449 gCol
= ucol_open(opt_locale
, &status
);
1450 if (U_FAILURE(status
)) {
1451 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
1455 if (status
==U_USING_DEFAULT_WARNING
&& opt_terse
==FALSE
) {
1456 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n", opt_locale
);
1458 if (status
==U_USING_FALLBACK_WARNING
&& opt_terse
==FALSE
) {
1459 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n", opt_locale
);
1463 ucol_setAttribute(gCol
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
1465 if (opt_french
&& opt_frenchoff
) {
1466 fprintf(stderr
, "collperf: Error, specified both -french and -frenchoff options.");
1470 ucol_setAttribute(gCol
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
1472 if (opt_frenchoff
) {
1473 ucol_setAttribute(gCol
, UCOL_FRENCH_COLLATION
, UCOL_OFF
, &status
);
1476 ucol_setAttribute(gCol
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
, &status
);
1479 ucol_setAttribute(gCol
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
, &status
);
1482 ucol_setAttribute(gCol
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
1485 ucol_setAttribute(gCol
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
1487 if (opt_level
!= 0) {
1488 switch (opt_level
) {
1490 ucol_setAttribute(gCol
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
1493 ucol_setAttribute(gCol
, UCOL_STRENGTH
, UCOL_SECONDARY
, &status
);
1496 ucol_setAttribute(gCol
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
1499 ucol_setAttribute(gCol
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
1502 ucol_setAttribute(gCol
, UCOL_STRENGTH
, UCOL_IDENTICAL
, &status
);
1505 fprintf(stderr
, "-level param must be between 1 and 5\n");
1510 if (U_FAILURE(status
)) {
1511 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
1517 // Set up a Windows LCID
1519 if (opt_langid
!= 0) {
1520 gWinLCID
= MAKELCID(opt_langid
, SORT_DEFAULT
);
1523 gWinLCID
= uloc_getLCID(opt_locale
);
1528 // Set the UNIX locale
1531 if (setlocale(LC_ALL
, opt_locale
) == 0) {
1532 fprintf(stderr
, "setlocale(LC_ALL, %s) failed.\n", opt_locale
);
1537 // Read in the input file.
1538 // File assumed to be utf-16.
1539 // Lines go onto heap buffers. Global index array to line starts is created.
1540 // Lines themselves are null terminated.
1543 UCharFile
f(opt_fName
);
1548 const int MAXLINES
= 100000;
1549 gFileLines
= new Line
[MAXLINES
];
1553 // Read the file, split into lines, and save in memory.
1554 // Loop runs once per utf-16 value from the input file,
1555 // (The number of bytes read from file per loop iteration depends on external encoding.)
1564 // We now have a good UTF-16 value in c.
1566 // Watch for CR, LF, EOF; these finish off a line.
1571 if (f
.eof() || c
== 0x0a || c
==0x2028) { // Unipad inserts 2028 line separators!
1574 gFileLines
[gNumFileLines
].name
= new UChar
[column
];
1575 gFileLines
[gNumFileLines
].len
= column
-1;
1576 memcpy(gFileLines
[gNumFileLines
].name
, buf
, column
* sizeof(UChar
));
1579 if (gNumFileLines
>= MAXLINES
) {
1580 fprintf(stderr
, "File too big. Max number of lines is %d\n", MAXLINES
);
1585 if (c
== 0xa || c
== 0x2028)
1593 static UBool warnFlag
= TRUE
;
1595 fprintf(stderr
, "Warning - file line longer than 1023 chars truncated.\n");
1602 if (opt_terse
== FALSE
) {
1603 printf("file \"%s\", %d lines.\n", opt_fName
, gNumFileLines
);
1607 // Convert the lines to the UNIX encoding.
1613 // Pre-compute ICU sort keys for the lines of the file.
1618 for (line
=0; line
<gNumFileLines
; line
++) {
1619 t
= ucol_getSortKey(gCol
, gFileLines
[line
].name
, -1, (unsigned char *)buf
, sizeof(buf
));
1620 gFileLines
[line
].icuSortKey
= new char[t
];
1622 if (t
> (int32_t)sizeof(buf
)) {
1623 t
= ucol_getSortKey(gCol
, gFileLines
[line
].name
, -1, (unsigned char *)gFileLines
[line
].icuSortKey
, t
);
1627 memcpy(gFileLines
[line
].icuSortKey
, buf
, t
);
1634 // Pre-compute Windows sort keys for the lines of the file.
1636 for (line
=0; line
<gNumFileLines
; line
++) {
1637 t
=LCMapStringW(gWinLCID
, LCMAP_SORTKEY
, gFileLines
[line
].name
, -1, buf
, sizeof(buf
));
1638 gFileLines
[line
].winSortKey
= new char[t
];
1639 if (t
> (int32_t)sizeof(buf
)) {
1640 t
= LCMapStringW(gWinLCID
, LCMAP_SORTKEY
, gFileLines
[line
].name
, -1, (unsigned short *)(gFileLines
[line
].winSortKey
), t
);
1644 memcpy(gFileLines
[line
].winSortKey
, buf
, t
);
1649 // Pre-compute UNIX sort keys for the lines of the file.
1652 for (line
=0; line
<gNumFileLines
; line
++) {
1653 t
=strxfrm((char *)buf
, gFileLines
[line
].unixName
, sizeof(buf
));
1654 gFileLines
[line
].unixSortKey
= new char[t
];
1655 if (t
> (int32_t)sizeof(buf
)) {
1656 t
= strxfrm(gFileLines
[line
].unixSortKey
, gFileLines
[line
].unixName
, sizeof(buf
));
1660 memcpy(gFileLines
[line
].unixSortKey
, buf
, t
);
1667 // Dump file lines, CEs, Sort Keys if requested.
1671 for (line
=0; line
<gNumFileLines
; line
++) {
1673 UChar c
= gFileLines
[line
].name
[i
];
1676 if (c
< 0x20 || c
> 0x7e) {
1677 printf("\\u%.4x", c
);
1686 UCollationElements
*CEiter
= ucol_openElements(gCol
, gFileLines
[line
].name
, -1, &status
);
1690 ce
= ucol_next(CEiter
, &status
);
1691 if (ce
== UCOL_NULLORDER
) {
1694 printf(" %.8x", ce
);
1701 ucol_closeElements(CEiter
);
1704 printf(" ICU Sort Key: ");
1706 unsigned char c
= gFileLines
[line
].icuSortKey
[i
];
1711 if (i
> 0 && i
% 20 == 0) {
1721 // Pre-sort the lines.
1724 gSortedLines
= new Line
*[gNumFileLines
];
1725 for (i
=0; i
<gNumFileLines
; i
++) {
1726 gSortedLines
[i
] = &gFileLines
[i
];
1730 qsort(gSortedLines
, gNumFileLines
, sizeof(Line
*), Winstrcmp
);
1732 else if (opt_unix
) {
1733 qsort(gSortedLines
, gNumFileLines
, sizeof(Line
*), UNIXstrcmp
);
1737 qsort(gSortedLines
, gNumFileLines
, sizeof(Line
*), ICUstrcmp
);
1742 // Make up a randomized order, will be used for sorting tests.
1744 gRandomLines
= new Line
*[gNumFileLines
];
1745 for (i
=0; i
<gNumFileLines
; i
++) {
1746 gRandomLines
[i
] = &gFileLines
[i
];
1748 qsort(gRandomLines
, gNumFileLines
, sizeof(Line
*), ICURandomCmp
);
1754 // We've got the file read into memory. Go do something with it.
1757 if (opt_qsort
) doQSort();
1758 if (opt_binsearch
) doBinarySearch();
1759 if (opt_keygen
) doKeyGen();
1760 if (opt_keyhist
) doKeyHist();
1761 if (opt_itertest
) doIterTest();