Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / tests / utf8-performance.c
1 /* GLIB - Library of useful routines for C programming
2  *
3  * Copyright (C) 2010 Mikhail Zabaluev <mikhail.zabaluev@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <string.h>
22
23 #include <glib.h>
24
25 #define NUM_ITERATIONS 500000
26
27 static const char str_ascii[] =
28     "The quick brown fox jumps over the lazy dog";
29
30 static const gchar str_latin1[] =
31     "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich";
32
33 /* Energizing GOELRO-talk in Russian, used by KDE */
34 static const char str_cyrillic[] =
35     "Широкая электрификация южных губерний даст мощный толчок подъёму "
36     "сельского хозяйства.";
37
38 /* First sentence from the Wikipedia article:
39  * http://zh.wikipedia.org/w/index.php?title=%E6%B1%89%E5%AD%97&oldid=13053137 */
40 static const char str_chinese[] =
41     "漢字,亦稱中文字、中国字,在台灣又被稱為國字,是漢字文化圈廣泛使用的一種文字,屬於表意文字的詞素音節文字";
42
43 typedef int (* GrindFunc) (const char *, gsize);
44
45 static int
46 grind_get_char (const char *str, gsize len)
47 {
48   gunichar acc = 0;
49   int i;
50   for (i = 0; i < NUM_ITERATIONS; i++)
51     {
52       const char *p = str;
53       while (*p) {
54         acc += g_utf8_get_char (p);
55         p = g_utf8_next_char (p);
56       }
57     }
58   return acc;
59 }
60
61 static int
62 grind_get_char_validated (const char *str, gsize len)
63 {
64   gunichar acc = 0;
65   int i;
66   for (i = 0; i < NUM_ITERATIONS; i++)
67     {
68       const char *p = str;
69       while (*p) {
70         acc += g_utf8_get_char_validated (p, -1);
71         p = g_utf8_next_char (p);
72       }
73     }
74   return acc;
75 }
76
77 static int
78 grind_utf8_to_ucs4 (const char *str, gsize len)
79 {
80   int i;
81   for (i = 0; i < NUM_ITERATIONS; i++)
82     {
83       gunichar *ustr;
84       ustr = g_utf8_to_ucs4 (str, -1, NULL, NULL, NULL);
85       g_free (ustr);
86     }
87   return 0;
88 }
89
90 static int
91 grind_get_char_backwards (const char *str, gsize len)
92 {
93   gunichar acc = 0;
94   int i;
95   for (i = 0; i < NUM_ITERATIONS; i++)
96     {
97       const char *p = str + len;
98       do
99         {
100           p = g_utf8_prev_char (p);
101           acc += g_utf8_get_char (p);
102         }
103       while (p != str);
104     }
105   return acc;
106 }
107
108 static int
109 grind_utf8_to_ucs4_sized (const char *str, gsize len)
110 {
111   int i;
112   for (i = 0; i < NUM_ITERATIONS; i++)
113     {
114       gunichar *ustr;
115       ustr = g_utf8_to_ucs4 (str, len, NULL, NULL, NULL);
116       g_free (ustr);
117     }
118   return 0;
119 }
120
121 static int
122 grind_utf8_to_ucs4_fast (const char *str, gsize len)
123 {
124   int i;
125   for (i = 0; i < NUM_ITERATIONS; i++)
126     {
127       gunichar *ustr;
128       ustr = g_utf8_to_ucs4_fast (str, -1, NULL);
129       g_free (ustr);
130     }
131   return 0;
132 }
133
134 static int
135 grind_utf8_to_ucs4_fast_sized (const char *str, gsize len)
136 {
137   int i;
138   for (i = 0; i < NUM_ITERATIONS; i++)
139     {
140       gunichar *ustr;
141       ustr = g_utf8_to_ucs4_fast (str, len, NULL);
142       g_free (ustr);
143     }
144   return 0;
145 }
146
147 static void
148 perform_for (GrindFunc grind_func, const char *str, const char *label)
149 {
150   gsize len;
151   gulong bytes_ground;
152   gdouble time_elapsed;
153   gdouble result;
154
155   len = strlen (str);
156   bytes_ground = (gulong) len * NUM_ITERATIONS;
157
158   g_test_timer_start ();
159
160   grind_func (str, len);
161
162   time_elapsed = g_test_timer_elapsed ();
163
164   result = ((gdouble) bytes_ground / time_elapsed) * 1.0e-6;
165
166   g_test_maximized_result (result, "%-9s %6.1f MB/s", label, result);
167 }
168
169 static void
170 perform (gconstpointer data)
171 {
172   GrindFunc grind_func = (GrindFunc) data;
173
174   if (!g_test_perf ())
175     return;
176
177   perform_for (grind_func, str_ascii, "ASCII:");
178   perform_for (grind_func, str_latin1, "Latin-1:");
179   perform_for (grind_func, str_cyrillic, "Cyrillic:");
180   perform_for (grind_func, str_chinese, "Chinese:");
181 }
182
183 int
184 main (int argc, char **argv)
185 {
186   g_test_init (&argc, &argv, NULL);
187   g_test_add_data_func ("/utf8/perf/get_char",
188       grind_get_char, perform);
189   g_test_add_data_func ("/utf8/perf/get_char-backwards",
190       grind_get_char_backwards, perform);
191   g_test_add_data_func ("/utf8/perf/get_char_validated",
192       grind_get_char_validated, perform);
193   g_test_add_data_func ("/utf8/perf/utf8_to_ucs4",
194       grind_utf8_to_ucs4, perform);
195   g_test_add_data_func ("/utf8/perf/utf8_to_ucs4-sized",
196       grind_utf8_to_ucs4_sized, perform);
197   g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast",
198       grind_utf8_to_ucs4_fast, perform);
199   g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast-sized",
200       grind_utf8_to_ucs4_fast_sized, perform);
201   return g_test_run ();
202 }