2 * Copyright (C) 2004 Red Hat, Inc., Matthias Clasen <mclasen@redhat.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
28 static gint repeats = 2;
29 static gint max_size = 8;
31 static GOptionEntry entries[] = {
32 { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
33 { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
38 typedef void (ClearFunc)(GtkTreeModel *model);
39 typedef void (InsertFunc)(GtkTreeModel *model,
44 list_store_append (GtkTreeModel *model,
48 GtkListStore *store = GTK_LIST_STORE (model);
52 text = g_strdup_printf ("row %d", i);
53 gtk_list_store_append (store, &iter);
54 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
59 list_store_prepend (GtkTreeModel *model,
63 GtkListStore *store = GTK_LIST_STORE (model);
67 text = g_strdup_printf ("row %d", i);
68 gtk_list_store_prepend (store, &iter);
69 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
74 list_store_insert (GtkTreeModel *model,
78 GtkListStore *store = GTK_LIST_STORE (model);
83 text = g_strdup_printf ("row %d", i);
84 n = g_random_int_range (0, i + 1);
85 gtk_list_store_insert (store, &iter, n);
86 gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
91 compare (GtkTreeModel *model,
99 gtk_tree_model_get (model, a, 1, &str_a, -1);
100 gtk_tree_model_get (model, b, 1, &str_b, -1);
102 result = strcmp (str_a, str_b);
111 tree_store_append (GtkTreeModel *model,
115 GtkTreeStore *store = GTK_TREE_STORE (model);
119 text = g_strdup_printf ("row %d", i);
120 gtk_tree_store_append (store, &iter, NULL);
121 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
126 tree_store_prepend (GtkTreeModel *model,
130 GtkTreeStore *store = GTK_TREE_STORE (model);
134 text = g_strdup_printf ("row %d", i);
135 gtk_tree_store_prepend (store, &iter, NULL);
136 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
141 tree_store_insert_flat (GtkTreeModel *model,
145 GtkTreeStore *store = GTK_TREE_STORE (model);
150 text = g_strdup_printf ("row %d", i);
151 n = g_random_int_range (0, i + 1);
152 gtk_tree_store_insert (store, &iter, NULL, n);
153 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
165 find_nth (GtkTreeModel *model,
170 FindData *fdata = (FindData *)data;
172 if (fdata->i >= fdata->n)
185 tree_store_insert_deep (GtkTreeModel *model,
189 GtkTreeStore *store = GTK_TREE_STORE (model);
194 text = g_strdup_printf ("row %d", i);
195 data.n = g_random_int_range (0, items);
199 gtk_tree_model_foreach (model, find_nth, &data);
200 gtk_tree_store_insert (store, &iter, data.found ? &(data.iter) : NULL, data.n);
201 gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
207 test_run (gchar *title,
215 int uordblks_before = 0, memused;
217 g_print ("%s (average over %d runs, time in milliseconds)\n"
218 "items \ttime \ttime/item \tused memory\n", title, repeats);
220 timer = g_timer_new ();
222 for (k = 0; k < max_size; k++)
226 for (d = 0; d < repeats; d++)
230 /* Peculiar location of this, btw. -- MW. */
231 uordblks_before = mallinfo().uordblks;
233 g_timer_reset (timer);
234 g_timer_start (timer);
235 for (i = 0; i < items; i++)
236 (*insert) (store, items, i);
237 g_timer_stop (timer);
238 elapsed += g_timer_elapsed (timer, NULL);
241 elapsed = elapsed * 1000 / repeats;
243 memused = (mallinfo().uordblks - uordblks_before) / 1024;
247 g_print ("%d \t%f \t%f \t%dk\n",
248 items, elapsed, elapsed/items, memused);
253 main (int argc, char *argv[])
257 gtk_init_with_args (&argc, &argv, NULL, entries, NULL, NULL);
259 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING));
261 test_run ("list store append",
263 (ClearFunc*)gtk_list_store_clear,
264 (InsertFunc*)list_store_append);
266 test_run ("list store prepend",
268 (ClearFunc*)gtk_list_store_clear,
269 (InsertFunc*)list_store_prepend);
271 test_run ("list store insert",
273 (ClearFunc*)gtk_list_store_clear,
274 (InsertFunc*)list_store_insert);
276 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
277 compare, NULL, NULL);
278 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
279 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
282 test_run ("list store insert (sorted)",
284 (ClearFunc*)gtk_list_store_clear,
285 (InsertFunc*)list_store_insert);
287 g_object_unref (model);
289 model = GTK_TREE_MODEL (gtk_tree_store_new (2, G_TYPE_INT, G_TYPE_STRING));
291 test_run ("tree store append",
293 (ClearFunc*)gtk_tree_store_clear,
294 (InsertFunc*)tree_store_append);
296 test_run ("tree store prepend",
298 (ClearFunc*)gtk_tree_store_clear,
299 (InsertFunc*)tree_store_prepend);
301 test_run ("tree store insert (flat)",
303 (ClearFunc*)gtk_tree_store_clear,
304 (InsertFunc*)tree_store_insert_flat);
306 test_run ("tree store insert (deep)",
308 (ClearFunc*)gtk_tree_store_clear,
309 (InsertFunc*)tree_store_insert_deep);
311 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
312 compare, NULL, NULL);
313 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
314 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
317 test_run ("tree store insert (flat, sorted)",
319 (ClearFunc*)gtk_tree_store_clear,
320 (InsertFunc*)tree_store_insert_flat);
322 test_run ("tree store insert (deep, sorted)",
324 (ClearFunc*)gtk_tree_store_clear,
325 (InsertFunc*)tree_store_insert_deep);