1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/* testtreemodel.c
* Copyright (C) 2004 Red Hat, Inc., Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#ifdef HAVE_MALLINFO
#include <malloc.h>
#endif
#include <gtk/gtk.h>
static int repeats = 2;
static int max_size = 8;
static GOptionEntry entries[] = {
{ "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
{ "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
{ NULL }
};
typedef void (ClearFunc)(GtkTreeModel *model);
typedef void (InsertFunc)(GtkTreeModel *model,
int items,
int i);
static void
list_store_append (GtkTreeModel *model,
int items,
int i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
char *text;
text = g_strdup_printf ("row %d", i);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
list_store_prepend (GtkTreeModel *model,
int items,
int i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
char *text;
text = g_strdup_printf ("row %d", i);
gtk_list_store_prepend (store, &iter);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
list_store_insert (GtkTreeModel *model,
int items,
int i)
{
GtkListStore *store = GTK_LIST_STORE (model);
GtkTreeIter iter;
char *text;
int n;
text = g_strdup_printf ("row %d", i);
n = g_random_int_range (0, i + 1);
gtk_list_store_insert (store, &iter, n);
gtk_list_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static int
compare (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer data)
{
char *str_a, *str_b;
int result;
gtk_tree_model_get (model, a, 1, &str_a, -1);
gtk_tree_model_get (model, b, 1, &str_b, -1);
result = strcmp (str_a, str_b);
g_free (str_a);
g_free (str_b);
return result;
}
static void
tree_store_append (GtkTreeModel *model,
int items,
int i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
char *text;
text = g_strdup_printf ("row %d", i);
gtk_tree_store_append (store, &iter, NULL);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
tree_store_prepend (GtkTreeModel *model,
int items,
int i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
char *text;
text = g_strdup_printf ("row %d", i);
gtk_tree_store_prepend (store, &iter, NULL);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
tree_store_insert_flat (GtkTreeModel *model,
int items,
int i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
char *text;
int n;
text = g_strdup_printf ("row %d", i);
n = g_random_int_range (0, i + 1);
gtk_tree_store_insert (store, &iter, NULL, n);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
typedef struct {
int i;
int n;
gboolean found;
GtkTreeIter iter;
} FindData;
static gboolean
find_nth (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data)
{
FindData *fdata = (FindData *)data;
if (fdata->i >= fdata->n)
{
fdata->iter = *iter;
fdata->found = TRUE;
return TRUE;
}
fdata->i++;
return FALSE;
}
static void
tree_store_insert_deep (GtkTreeModel *model,
int items,
int i)
{
GtkTreeStore *store = GTK_TREE_STORE (model);
GtkTreeIter iter;
char *text;
FindData data;
text = g_strdup_printf ("row %d", i);
data.n = g_random_int_range (0, items);
data.i = 0;
data.found = FALSE;
if (data.n < i)
gtk_tree_model_foreach (model, find_nth, &data);
gtk_tree_store_insert (store, &iter, data.found ? &(data.iter) : NULL, data.n);
gtk_tree_store_set (store, &iter, 0, i, 1, text, -1);
g_free (text);
}
static void
test_run (const char *title,
GtkTreeModel *store,
ClearFunc *clear,
InsertFunc *insert)
{
int i, k, d, items;
GTimer *timer;
double elapsed;
int memused;
#ifdef HAVE_MALLINFO
int uordblks_before = 0;
#endif
g_print ("%s (average over %d runs, time in milliseconds)\n"
"items \ttime \ttime/item \tused memory\n", title, repeats);
timer = g_timer_new ();
for (k = 0; k < max_size; k++)
{
items = 1 << k;
elapsed = 0.0;
for (d = 0; d < repeats; d++)
{
(*clear)(store);
#ifdef HAVE_MALLINFO
/* Peculiar location of this, btw. -- MW. */
uordblks_before = mallinfo().uordblks;
#endif
g_timer_reset (timer);
g_timer_start (timer);
for (i = 0; i < items; i++)
(*insert) (store, items, i);
g_timer_stop (timer);
elapsed += g_timer_elapsed (timer, NULL);
}
elapsed = elapsed * 1000 / repeats;
#ifdef HAVE_MALLINFO
memused = (mallinfo().uordblks - uordblks_before) / 1024;
#else
memused = 0;
#endif
g_print ("%d \t%f \t%f \t%dk\n",
items, elapsed, elapsed/items, memused);
}
}
int
main (int argc, char *argv[])
{
GtkTreeModel *model;
GOptionContext *context;
context = g_option_context_new ("");
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
gtk_init ();
model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING));
test_run ("list store append",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_append);
test_run ("list store prepend",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_prepend);
test_run ("list store insert",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_insert);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
compare, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
test_run ("list store insert (sorted)",
model,
(ClearFunc*)gtk_list_store_clear,
(InsertFunc*)list_store_insert);
g_object_unref (model);
model = GTK_TREE_MODEL (gtk_tree_store_new (2, G_TYPE_INT, G_TYPE_STRING));
test_run ("tree store append",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_append);
test_run ("tree store prepend",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_prepend);
test_run ("tree store insert (flat)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_flat);
test_run ("tree store insert (deep)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_deep);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (model),
compare, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
test_run ("tree store insert (flat, sorted)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_flat);
test_run ("tree store insert (deep, sorted)",
model,
(ClearFunc*)gtk_tree_store_clear,
(InsertFunc*)tree_store_insert_deep);
return 0;
}
|