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/>.
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
);
44 grind_get_char (const char *str
, gsize len
)
48 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
52 acc
+= g_utf8_get_char (p
);
53 p
= g_utf8_next_char (p
);
60 grind_get_char_validated (const char *str
, gsize len
)
64 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
68 acc
+= g_utf8_get_char_validated (p
, -1);
69 p
= g_utf8_next_char (p
);
76 grind_utf8_to_ucs4 (const char *str
, gsize len
)
79 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
82 ustr
= g_utf8_to_ucs4 (str
, -1, NULL
, NULL
, NULL
);
89 grind_get_char_backwards (const char *str
, gsize len
)
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
);
107 grind_utf8_to_ucs4_sized (const char *str
, gsize len
)
110 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
113 ustr
= g_utf8_to_ucs4 (str
, len
, NULL
, NULL
, NULL
);
120 grind_utf8_to_ucs4_fast (const char *str
, gsize len
)
123 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
126 ustr
= g_utf8_to_ucs4_fast (str
, -1, NULL
);
133 grind_utf8_to_ucs4_fast_sized (const char *str
, gsize len
)
136 for (i
= 0; i
< NUM_ITERATIONS
; i
++)
139 ustr
= g_utf8_to_ucs4_fast (str
, len
, NULL
);
146 perform_for (GrindFunc grind_func
, const char *str
, const char *label
)
150 gdouble time_elapsed
;
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
);
168 perform (gconstpointer data
)
170 GrindFunc grind_func
= (GrindFunc
) data
;
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
);
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 ();