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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
30 static gint repeats
= 2;
31 static gint max_size
= 8;
33 static GOptionEntry entries
[] = {
34 { "repeats", 'r', 0, G_OPTION_ARG_INT
, &repeats
, "Average over N repetitions", "N" },
35 { "max-size", 'm', 0, G_OPTION_ARG_INT
, &max_size
, "Test up to 2^M items", "M" },
40 typedef void (ClearFunc
)(GtkTreeModel
*model
);
41 typedef void (InsertFunc
)(GtkTreeModel
*model
,
46 list_store_append (GtkTreeModel
*model
,
50 GtkListStore
*store
= GTK_LIST_STORE (model
);
54 text
= g_strdup_printf ("row %d", i
);
55 gtk_list_store_append (store
, &iter
);
56 gtk_list_store_set (store
, &iter
, 0, i
, 1, text
, -1);
61 list_store_prepend (GtkTreeModel
*model
,
65 GtkListStore
*store
= GTK_LIST_STORE (model
);
69 text
= g_strdup_printf ("row %d", i
);
70 gtk_list_store_prepend (store
, &iter
);
71 gtk_list_store_set (store
, &iter
, 0, i
, 1, text
, -1);
76 list_store_insert (GtkTreeModel
*model
,
80 GtkListStore
*store
= GTK_LIST_STORE (model
);
85 text
= g_strdup_printf ("row %d", i
);
86 n
= g_random_int_range (0, i
+ 1);
87 gtk_list_store_insert (store
, &iter
, n
);
88 gtk_list_store_set (store
, &iter
, 0, i
, 1, text
, -1);
93 compare (GtkTreeModel
*model
,
101 gtk_tree_model_get (model
, a
, 1, &str_a
, -1);
102 gtk_tree_model_get (model
, b
, 1, &str_b
, -1);
104 result
= strcmp (str_a
, str_b
);
113 tree_store_append (GtkTreeModel
*model
,
117 GtkTreeStore
*store
= GTK_TREE_STORE (model
);
121 text
= g_strdup_printf ("row %d", i
);
122 gtk_tree_store_append (store
, &iter
, NULL
);
123 gtk_tree_store_set (store
, &iter
, 0, i
, 1, text
, -1);
128 tree_store_prepend (GtkTreeModel
*model
,
132 GtkTreeStore
*store
= GTK_TREE_STORE (model
);
136 text
= g_strdup_printf ("row %d", i
);
137 gtk_tree_store_prepend (store
, &iter
, NULL
);
138 gtk_tree_store_set (store
, &iter
, 0, i
, 1, text
, -1);
143 tree_store_insert_flat (GtkTreeModel
*model
,
147 GtkTreeStore
*store
= GTK_TREE_STORE (model
);
152 text
= g_strdup_printf ("row %d", i
);
153 n
= g_random_int_range (0, i
+ 1);
154 gtk_tree_store_insert (store
, &iter
, NULL
, n
);
155 gtk_tree_store_set (store
, &iter
, 0, i
, 1, text
, -1);
167 find_nth (GtkTreeModel
*model
,
172 FindData
*fdata
= (FindData
*)data
;
174 if (fdata
->i
>= fdata
->n
)
187 tree_store_insert_deep (GtkTreeModel
*model
,
191 GtkTreeStore
*store
= GTK_TREE_STORE (model
);
196 text
= g_strdup_printf ("row %d", i
);
197 data
.n
= g_random_int_range (0, items
);
201 gtk_tree_model_foreach (model
, find_nth
, &data
);
202 gtk_tree_store_insert (store
, &iter
, data
.found
? &(data
.iter
) : NULL
, data
.n
);
203 gtk_tree_store_set (store
, &iter
, 0, i
, 1, text
, -1);
209 test_run (gchar
*title
,
217 int uordblks_before
= 0, memused
;
219 g_print ("%s (average over %d runs, time in milliseconds)\n"
220 "items \ttime \ttime/item \tused memory\n", title
, repeats
);
222 timer
= g_timer_new ();
224 for (k
= 0; k
< max_size
; k
++)
228 for (d
= 0; d
< repeats
; d
++)
232 /* Peculiar location of this, btw. -- MW. */
233 uordblks_before
= mallinfo().uordblks
;
235 g_timer_reset (timer
);
236 g_timer_start (timer
);
237 for (i
= 0; i
< items
; i
++)
238 (*insert
) (store
, items
, i
);
239 g_timer_stop (timer
);
240 elapsed
+= g_timer_elapsed (timer
, NULL
);
243 elapsed
= elapsed
* 1000 / repeats
;
245 memused
= (mallinfo().uordblks
- uordblks_before
) / 1024;
249 g_print ("%d \t%f \t%f \t%dk\n",
250 items
, elapsed
, elapsed
/items
, memused
);
255 main (int argc
, char *argv
[])
259 gtk_init_with_args (&argc
, &argv
, NULL
, entries
, NULL
, NULL
);
261 model
= GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT
, G_TYPE_STRING
));
263 test_run ("list store append",
265 (ClearFunc
*)gtk_list_store_clear
,
266 (InsertFunc
*)list_store_append
);
268 test_run ("list store prepend",
270 (ClearFunc
*)gtk_list_store_clear
,
271 (InsertFunc
*)list_store_prepend
);
273 test_run ("list store insert",
275 (ClearFunc
*)gtk_list_store_clear
,
276 (InsertFunc
*)list_store_insert
);
278 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model
),
279 compare
, NULL
, NULL
);
280 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model
),
281 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
284 test_run ("list store insert (sorted)",
286 (ClearFunc
*)gtk_list_store_clear
,
287 (InsertFunc
*)list_store_insert
);
289 g_object_unref (model
);
291 model
= GTK_TREE_MODEL (gtk_tree_store_new (2, G_TYPE_INT
, G_TYPE_STRING
));
293 test_run ("tree store append",
295 (ClearFunc
*)gtk_tree_store_clear
,
296 (InsertFunc
*)tree_store_append
);
298 test_run ("tree store prepend",
300 (ClearFunc
*)gtk_tree_store_clear
,
301 (InsertFunc
*)tree_store_prepend
);
303 test_run ("tree store insert (flat)",
305 (ClearFunc
*)gtk_tree_store_clear
,
306 (InsertFunc
*)tree_store_insert_flat
);
308 test_run ("tree store insert (deep)",
310 (ClearFunc
*)gtk_tree_store_clear
,
311 (InsertFunc
*)tree_store_insert_deep
);
313 gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model
),
314 compare
, NULL
, NULL
);
315 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model
),
316 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID
,
319 test_run ("tree store insert (flat, sorted)",
321 (ClearFunc
*)gtk_tree_store_clear
,
322 (InsertFunc
*)tree_store_insert_flat
);
324 test_run ("tree store insert (deep, sorted)",
326 (ClearFunc
*)gtk_tree_store_clear
,
327 (InsertFunc
*)tree_store_insert_deep
);