Kjell Morgenstern a écrit :
I forgot the attatchment in the previous mail, so this is the next try.

Hello!

I noticed that starting a random slide show might take some time (up
to some minutes, depending on the amount of images and the machine).

Looking into the code showed that a list was used to prepare another
random shuffled list, so it was an O(n^2) algorithm. I changed this to
use an array (similar to STLs random_shuffle) and got a significant
performance improvement: Starting time of random or non-random
slideshows is now almost the same (seconds instead of minutes).

I attatched a patch for slideshow.c in this mail. The patch is based
on svn revision 1375.

Kjell
Nice patch, the improvement is very significant.

Here is a slightly cleaner version, which matches current geeqie
coding style better.

A document named CODING at the root of the source tree indicates
few points to take care of.
Please use --enable-developer configure option to increase the compiler
verbosity (ie. about unused variables and more).

I'll commit it soon if no one disagrees.

Thank a lot for contributing to Geeqie project !

--
Laurent Monin aka Zas
Index: slideshow.c
===================================================================
--- slideshow.c	(révision 1375)
+++ slideshow.c	(copie de travail)
@@ -66,22 +66,57 @@
 	return list;
 }
 
+static void ptr_array_add(gpointer data, GPtrArray *array)
+{
+	g_ptr_array_add(array, data);
+}
+
+static void list_prepend(gpointer data, GList **list)
+{
+	*list = g_list_prepend(*list, data);
+}
+
+static GPtrArray *generate_ptr_array_from_list(GList *src_list)
+{
+	GPtrArray *arr = g_ptr_array_sized_new(g_list_length(src_list));
+
+	g_list_foreach(src_list, (GFunc) ptr_array_add, arr);
+
+	return arr;
+}
+
+static void swap(GPtrArray *array, guint index1, guint index2)
+{
+	gpointer temp = g_ptr_array_index(array, index1);
+	
+	g_ptr_array_index(array, index1) = g_ptr_array_index(array, index2);
+	g_ptr_array_index(array, index2) = temp;
+}
+
+static void ptr_array_random_shuffle(GPtrArray *array)
+{
+	guint i;
+	for (i = 0; i < array->len; ++i)
+		{
+		guint p = (double)rand() / ((double)RAND_MAX + 1.0) * array->len;
+		swap(array, i, p);
+		}
+}
+
 static GList *generate_random_list(SlideShowData *ss)
 {
-	GList *src_list = NULL;
+	GList *src_list;
+	GPtrArray *src_array;
 	GList *list = NULL;
-	GList *work;
 
 	src_list = generate_list(ss);
+	src_array = generate_ptr_array_from_list(src_list);
+	g_list_free(src_list);
 
-	while (src_list)
-		{
-		gint p = (gdouble)rand() / ((gdouble)RAND_MAX + 1.0) * g_list_length(src_list);
-		work = g_list_nth(src_list, p);
-		list = g_list_prepend(list, work->data);
-		src_list = g_list_remove(src_list, work->data);
-		}
-
+	ptr_array_random_shuffle(src_array);
+	g_ptr_array_foreach(src_array, (GFunc) list_prepend, &list);
+	g_ptr_array_free(src_array, TRUE);
+	
 	return list;
 }
 
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Geeqie-devel mailing list
Geeqie-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geeqie-devel

Reply via email to