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