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_han
[] =
39 "漢字,亦稱中文字、中国字,在台灣又被稱為國字,是漢字文化圈廣泛使用的一種文字,屬於表意文字的詞素音節文字";
41 typedef int (* GrindFunc
) (const char *, gsize
);
43 #define GRIND_LOOP_BEGIN \
46 for (i = 0; i < NUM_ITERATIONS; i++)
48 #define GRIND_LOOP_END \
52 grind_get_char (const char *str
, gsize len
)
60 acc
+= g_utf8_get_char (p
);
61 p
= g_utf8_next_char (p
);
69 grind_get_char_validated (const char *str
, gsize len
)
77 acc
+= g_utf8_get_char_validated (p
, -1);
78 p
= g_utf8_next_char (p
);
86 grind_utf8_to_ucs4 (const char *str
, gsize len
)
91 ustr
= g_utf8_to_ucs4 (str
, -1, NULL
, NULL
, NULL
);
99 grind_get_char_backwards (const char *str
, gsize len
)
104 const char *p
= str
+ len
;
107 p
= g_utf8_prev_char (p
);
108 acc
+= g_utf8_get_char (p
);
117 grind_utf8_to_ucs4_sized (const char *str
, gsize len
)
122 ustr
= g_utf8_to_ucs4 (str
, len
, NULL
, NULL
, NULL
);
130 grind_utf8_to_ucs4_fast (const char *str
, gsize len
)
135 ustr
= g_utf8_to_ucs4_fast (str
, -1, NULL
);
143 grind_utf8_to_ucs4_fast_sized (const char *str
, gsize len
)
148 ustr
= g_utf8_to_ucs4_fast (str
, len
, NULL
);
156 grind_utf8_validate (const char *str
, gsize len
)
159 g_utf8_validate (str
, -1, NULL
);
165 grind_utf8_validate_sized (const char *str
, gsize len
)
168 g_utf8_validate (str
, len
, NULL
);
173 typedef struct _GrindData
{
179 perform (gconstpointer data
)
181 GrindData
*gd
= (GrindData
*) data
;
182 GrindFunc grind_func
= gd
->func
;
183 const char *str
= gd
->str
;
186 gdouble time_elapsed
;
190 bytes_ground
= (gulong
) len
* NUM_ITERATIONS
;
192 g_test_timer_start ();
194 grind_func (str
, len
);
196 time_elapsed
= g_test_timer_elapsed ();
198 result
= ((gdouble
) bytes_ground
/ time_elapsed
) * 1.0e-6;
200 g_test_maximized_result (result
, "%7.1f MB/s", result
);
202 g_slice_free (GrindData
, gd
);
206 add_cases(const char *path
, GrindFunc func
)
208 #define ADD_CASE(script) \
212 gd = g_slice_new0(GrindData); \
214 gd->str = str_##script; \
215 full_path = g_strdup_printf("%s/" #script, path); \
216 g_test_add_data_func (full_path, gd, perform); \
217 g_free (full_path); \
229 main (int argc
, char **argv
)
231 g_test_init (&argc
, &argv
, NULL
);
235 add_cases ("/utf8/perf/get_char", grind_get_char
);
236 add_cases ("/utf8/perf/get_char-backwards", grind_get_char_backwards
);
237 add_cases ("/utf8/perf/get_char_validated", grind_get_char_validated
);
238 add_cases ("/utf8/perf/utf8_to_ucs4", grind_utf8_to_ucs4
);
239 add_cases ("/utf8/perf/utf8_to_ucs4-sized", grind_utf8_to_ucs4_sized
);
240 add_cases ("/utf8/perf/utf8_to_ucs4_fast", grind_utf8_to_ucs4_fast
);
241 add_cases ("/utf8/perf/utf8_to_ucs4_fast-sized", grind_utf8_to_ucs4_fast_sized
);
242 add_cases ("/utf8/perf/utf8_validate", grind_utf8_validate
);
243 add_cases ("/utf8/perf/utf8_validate-sized", grind_utf8_validate_sized
);
246 return g_test_run ();