xref: /haiku/src/tests/kits/locale/collatorSpeed.cpp (revision 4d8811742fa447ec05b4993a16a0931bc29aafab)
1 /*
2 ** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 
7 #include <Collator.h>
8 #include <Locale.h>
9 #include <StopWatch.h>
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 
15 
16 const char *kStrings[] = {
17 	"a-b-c",
18 	"a b c",
19 	"A.b.c",
20 	"ä,b,c",
21 	"abc",
22 	"gehen",
23 	"géhen",
24 	"aus",
25 	"äUß",
26 	"auss",
27 	"WO",
28 	"wÖ",
29 	"SO",
30 	"so",
31 	"açñ",
32 	"acn",
33 	"pêche",
34 	"pêché",
35 	"peché",
36 	"peche",
37 	"pecher",
38 	"eñe",
39 	"ene",
40 	"nz",
41 	"ña",
42 	"llamar",
43 	"luz",
44 };
45 const uint32 kNumStrings = sizeof(kStrings) / sizeof(kStrings[0]);
46 const uint32 kIterations = 50000;
47 
48 
49 void
50 test(BCollator *collator, const char *name, int8 strength)
51 {
52 	collator->SetDefaultStrength(strength);
53 
54 	BStopWatch watch(name, true);
55 
56 	for (uint32 j = 0; j < kIterations; j++) {
57 		for (uint32 i = 0; i < kNumStrings; i++) {
58 			BString key;
59 			collator->GetSortKey(kStrings[i], &key);
60 		}
61 	}
62 
63 	watch.Suspend();
64 	double secs = watch.ElapsedTime() / 1000000.0;
65 	printf("\t%s%9Ld usecs, %6.3g secs,%9lu keys/s\n",
66 		name, watch.ElapsedTime(), secs, uint32(kIterations * kNumStrings / secs));
67 }
68 
69 
70 void
71 usage()
72 {
73 	fprintf(stderr,
74 		"usage: collatorSpeed [-i] [<add-on path>]\n"
75 		"  -i\tignore punctuation (defaults to: punctuation matters)\n");
76 	exit(-1);
77 }
78 
79 
80 int
81 main(int argc, char **argv)
82 {
83 	// Parse command line arguments
84 
85 	bool ignorePunctuation = false;
86 	char *addon = NULL;
87 	BCollator *collator = NULL;
88 
89 	while ((++argv)[0]) {
90 		if (argv[0][0] == '-') {
91 			if (!strcmp(argv[0], "-i"))
92 				ignorePunctuation = true;
93 			else if (!strcmp(argv[0], "--help"))
94 				usage();
95 		} else {
96 			// this will be the add-on to be loaded
97 			addon = argv[0];
98 		}
99 	}
100 
101 	// load the collator add-on if necessary
102 
103 	if (addon != NULL) {
104 		image_id image = load_add_on(addon);
105 		if (image < B_OK)
106 			fprintf(stderr, "could not load add-on at \"%s\": %s.\n", addon, strerror(image));
107 
108 		BCollatorAddOn *(*instantiate)(void);
109 		if (get_image_symbol(image, "instantiate_collator",
110 				B_SYMBOL_TYPE_TEXT, (void **)&instantiate) == B_OK) {
111 			BCollatorAddOn *collatorAddOn = instantiate();
112 			if (collatorAddOn != NULL)
113 				collator = new BCollator(collatorAddOn, B_COLLATE_PRIMARY, true);
114 		} else if (image >= B_OK) {
115 			fprintf(stderr, "could not find instantiate_collator() function in add-on!\n");
116 			unload_add_on(image);
117 		}
118 	}
119 
120 	if (collator == NULL) {
121 		collator = be_locale->Collator();
122 		addon = (char*)"default";
123 	}
124 
125 	// test key creation speed
126 
127 	collator->SetIgnorePunctuation(ignorePunctuation);
128 
129 	printf("%s:\n", addon);
130 	test(collator, "primary:   ", B_COLLATE_PRIMARY);
131 	test(collator, "secondary: ", B_COLLATE_SECONDARY);
132 	test(collator, "tertiary:  ", B_COLLATE_TERTIARY);
133 	test(collator, "quaternary:", B_COLLATE_QUATERNARY);
134 	test(collator, "identical: ", B_COLLATE_IDENTICAL);
135 
136 	return 0;
137 }
138 
139