« Return to Thread: [patch 00/13] [RFC] Hashtable improvements and API changes

[patch 01/13] Speed up osync_db database queries

by Daniel Gollub :: Rate this Message:

Reply to Author | View in Thread

g_list_append calls g_list_last() internally, which is traverse the entire list
every time. Calling g_list_prepend() and later reverse the entire list, is more
efficient. good: O(2N) bad: O(N²)
---
 opensync/db/opensync_db.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Index: opensync/opensync/db/opensync_db.c
===================================================================
--- opensync.orig/opensync/db/opensync_db.c
+++ opensync/opensync/db/opensync_db.c
@@ -189,11 +189,19 @@ GList *osync_db_query_table(OSyncDB *db,
  for (j=0; j < numrows; j++) {
  GList *row = NULL;
  for (i=0; i < numcolumns; i++)
- row = g_list_append(row, g_strdup(result[column_count++]));
+ /* speed up - prepend instead of append */
+ row = g_list_prepend(row, g_strdup(result[column_count++]));
 
- table = g_list_append(table, row);
+ /* items got prepended, reverse the list again */
+ row = g_list_reverse(row);
+
+ /* speed up - prepend instead of append. */
+ table = g_list_prepend(table, row);
  }
 
+ /* items got prepended, reverse the list again */
+ table = g_list_reverse(table);
+
  sqlite3_free_table(result);
 
  osync_trace(TRACE_EXIT, "%s: %p", __func__, table);



-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Opensync-devel mailing list
Opensync-devel@...
https://lists.sourceforge.net/lists/listinfo/opensync-devel

 « Return to Thread: [patch 00/13] [RFC] Hashtable improvements and API changes

LightInTheBox - Buy quality products at wholesale price