diff options
author | Matthias Clasen <mclasen@redhat.com> | 2020-07-01 14:04:43 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2020-07-12 20:54:46 -0400 |
commit | 825b2a4139756d9f489fac6e424fbcb034ef851b (patch) | |
tree | 13ac1f25ec2f15494fff1e9a73a2025b528abeec | |
parent | 8a23649d40f14733fd01aaf9fe8475cdee84c9e3 (diff) | |
download | gtk+-wip/matthiasc/stringlist-array.tar.gz |
wip: benchmarkswip/matthiasc/stringlist-array
-rw-r--r-- | testsuite/gtk/listmodel-performance.c | 91 | ||||
-rw-r--r-- | testsuite/gtk/meson.build | 1 |
2 files changed, 92 insertions, 0 deletions
diff --git a/testsuite/gtk/listmodel-performance.c b/testsuite/gtk/listmodel-performance.c new file mode 100644 index 0000000000..e8654db12f --- /dev/null +++ b/testsuite/gtk/listmodel-performance.c @@ -0,0 +1,91 @@ +#include <gtk/gtk.h> + +static GObject * +get_object (const char *string) +{ + GtkStringList *list; + GObject *obj; + + list = gtk_string_list_new ((const char *[]){string, NULL}); + obj = g_list_model_get_item (G_LIST_MODEL (list), 0); + g_object_unref (list); + + return obj; +} + +static GListModel * +make_store (guint n_items) +{ + GListStore *store; + guint i; + + store = g_list_store_new (GTK_TYPE_STRING_OBJECT); + + for (i = 0; i < n_items; i++) + { + char *string; + GObject *obj; + + string = g_strdup_printf ("item %d", i); + obj = get_object (string); + g_list_store_append (store, obj); + g_object_unref (obj); + g_free (string); + } + + return G_LIST_MODEL (store); +} + +static void +do_random_access (guint size) +{ + GListModel *model; + guint i; + guint position; + GtkStringObject *obj; + gint64 start, end; + guint iterations = 10000000; + + model = make_store (size); + + start = g_get_monotonic_time (); + + for (i = 0; i < iterations; i++) + { + position = g_random_int_range (0, size); + obj = g_list_model_get_item (model, position); + if (g_getenv ("PRINT_ACCESS")) + g_print ("%s", gtk_string_object_get_string (obj)); + g_object_unref (obj); + } + + end = g_get_monotonic_time (); + + g_print ("size %u: %g accesses per second\n", + size, + (iterations / (double) G_TIME_SPAN_SECOND) * ((double)(end - start))); + + g_object_unref (model); +} + +static void +random_access (void) +{ + do_random_access (1e1); + do_random_access (1e2); + do_random_access (1e3); + do_random_access (1e4); + do_random_access (1e5); + do_random_access (1e6); + do_random_access (1e7); +} + +int +main (int argc, char *argv[]) +{ + gtk_test_init (&argc, &argv); + + g_test_add_func ("/liststore/random-access", random_access); + + return g_test_run (); +} diff --git a/testsuite/gtk/meson.build b/testsuite/gtk/meson.build index cee96704db..26ed064f5d 100644 --- a/testsuite/gtk/meson.build +++ b/testsuite/gtk/meson.build @@ -76,6 +76,7 @@ tests = [ ['revealer-size'], ['widgetorder'], ['widget-refcount'], + ['listmodel-performance'], ] # Tests that are expected to fail |