Updated Hungarian translation
[glib.git] / glib / tests / utf8-performance.c
blob20e5e0215064816b973bcfebf68f51b06675d304
1 /* GLIB - Library of useful routines for C programming
3 * Copyright (C) 2010 Mikhail Zabaluev <mikhail.zabaluev@gmail.com>
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.
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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <string.h>
21 #include <glib.h>
23 #define NUM_ITERATIONS 500000
25 static const char str_ascii[] =
26 "The quick brown fox jumps over the lazy dog";
28 static const gchar str_latin1[] =
29 "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich";
31 /* Energizing GOELRO-talk in Russian, used by KDE */
32 static const char str_cyrillic[] =
33 "Широкая электрификация южных губерний даст мощный толчок подъёму "
34 "сельского хозяйства.";
36 /* First sentence from the Wikipedia article:
37 * http://zh.wikipedia.org/w/index.php?title=%E6%B1%89%E5%AD%97&oldid=13053137 */
38 static const char str_chinese[] =
39 "漢字,亦稱中文字、中国字,在台灣又被稱為國字,是漢字文化圈廣泛使用的一種文字,屬於表意文字的詞素音節文字";
41 typedef int (* GrindFunc) (const char *, gsize);
43 static int
44 grind_get_char (const char *str, gsize len)
46 gunichar acc = 0;
47 int i;
48 for (i = 0; i < NUM_ITERATIONS; i++)
50 const char *p = str;
51 while (*p) {
52 acc += g_utf8_get_char (p);
53 p = g_utf8_next_char (p);
56 return acc;
59 static int
60 grind_get_char_validated (const char *str, gsize len)
62 gunichar acc = 0;
63 int i;
64 for (i = 0; i < NUM_ITERATIONS; i++)
66 const char *p = str;
67 while (*p) {
68 acc += g_utf8_get_char_validated (p, -1);
69 p = g_utf8_next_char (p);
72 return acc;
75 static int
76 grind_utf8_to_ucs4 (const char *str, gsize len)
78 int i;
79 for (i = 0; i < NUM_ITERATIONS; i++)
81 gunichar *ustr;
82 ustr = g_utf8_to_ucs4 (str, -1, NULL, NULL, NULL);
83 g_free (ustr);
85 return 0;
88 static int
89 grind_get_char_backwards (const char *str, gsize len)
91 gunichar acc = 0;
92 int i;
93 for (i = 0; i < NUM_ITERATIONS; i++)
95 const char *p = str + len;
98 p = g_utf8_prev_char (p);
99 acc += g_utf8_get_char (p);
101 while (p != str);
103 return acc;
106 static int
107 grind_utf8_to_ucs4_sized (const char *str, gsize len)
109 int i;
110 for (i = 0; i < NUM_ITERATIONS; i++)
112 gunichar *ustr;
113 ustr = g_utf8_to_ucs4 (str, len, NULL, NULL, NULL);
114 g_free (ustr);
116 return 0;
119 static int
120 grind_utf8_to_ucs4_fast (const char *str, gsize len)
122 int i;
123 for (i = 0; i < NUM_ITERATIONS; i++)
125 gunichar *ustr;
126 ustr = g_utf8_to_ucs4_fast (str, -1, NULL);
127 g_free (ustr);
129 return 0;
132 static int
133 grind_utf8_to_ucs4_fast_sized (const char *str, gsize len)
135 int i;
136 for (i = 0; i < NUM_ITERATIONS; i++)
138 gunichar *ustr;
139 ustr = g_utf8_to_ucs4_fast (str, len, NULL);
140 g_free (ustr);
142 return 0;
145 static void
146 perform_for (GrindFunc grind_func, const char *str, const char *label)
148 gsize len;
149 gulong bytes_ground;
150 gdouble time_elapsed;
151 gdouble result;
153 len = strlen (str);
154 bytes_ground = (gulong) len * NUM_ITERATIONS;
156 g_test_timer_start ();
158 grind_func (str, len);
160 time_elapsed = g_test_timer_elapsed ();
162 result = ((gdouble) bytes_ground / time_elapsed) * 1.0e-6;
164 g_test_maximized_result (result, "%-9s %6.1f MB/s", label, result);
167 static void
168 perform (gconstpointer data)
170 GrindFunc grind_func = (GrindFunc) data;
172 if (!g_test_perf ())
173 return;
175 perform_for (grind_func, str_ascii, "ASCII:");
176 perform_for (grind_func, str_latin1, "Latin-1:");
177 perform_for (grind_func, str_cyrillic, "Cyrillic:");
178 perform_for (grind_func, str_chinese, "Chinese:");
182 main (int argc, char **argv)
184 g_test_init (&argc, &argv, NULL);
186 if (g_test_perf ())
188 g_test_add_data_func ("/utf8/perf/get_char", grind_get_char, perform);
189 g_test_add_data_func ("/utf8/perf/get_char-backwards", grind_get_char_backwards, perform);
190 g_test_add_data_func ("/utf8/perf/get_char_validated", grind_get_char_validated, perform);
191 g_test_add_data_func ("/utf8/perf/utf8_to_ucs4", grind_utf8_to_ucs4, perform);
192 g_test_add_data_func ("/utf8/perf/utf8_to_ucs4-sized", grind_utf8_to_ucs4_sized, perform);
193 g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast", grind_utf8_to_ucs4_fast, perform);
194 g_test_add_data_func ("/utf8/perf/utf8_to_ucs4_fast-sized", grind_utf8_to_ucs4_fast_sized, perform);
197 return g_test_run ();