Re: g_strjoinv() is very slow



On Sat, Sep 23, 2000 at 12:40:32PM -0400, Havoc Pennington wrote:
> 
> David Odin <David Odin bigfoot com> writes: 
> >   Please have a look at it. I can make a patch against glib-1.2.8 or against
> > current CVS HEAD.
> >
> 
> Should be against HEAD. (I didn't look at the implementation in
> detail, but I can go ahead and answer that much. ;-)
> 
   OK. Attached.

   I also think the same optimisations could be done with g_strjoin() and
g_strconcat(). I'll have a look.

          Regards,

                  DindinX

-- 
David Odin bigfoot com
--- glib/gstrfuncs.c.orig	Sat Sep 23 18:39:56 2000
+++ glib/gstrfuncs.c	Sat Sep 23 18:44:02 2000
@@ -1551,9 +1551,10 @@
 
 gchar*
 g_strjoinv (const gchar  *separator,
-	    gchar       **str_array)
+            gchar       **str_array)
 {
   gchar *string;
+  gchar *ptr;
 
   g_return_val_if_fail (str_array != NULL, NULL);
 
@@ -1567,18 +1568,21 @@
 
       separator_len = strlen (separator);
       len = 1 + strlen (str_array[0]);
-      for(i = 1; str_array[i] != NULL; i++)
-	len += separator_len + strlen(str_array[i]);
+      for (i = 1; str_array[i] != NULL; i++)
+        len += strlen(str_array[i]);
+      len += separator_len*i;
 
       string = g_new (gchar, len);
       *string = 0;
       strcat (string, *str_array);
+      ptr = string + strlen (*str_array);
       for (i = 1; str_array[i] != NULL; i++)
-	{
-	  strcat (string, separator);
-	  strcat (string, str_array[i]);
-	}
-      }
+        {
+          memcpy (ptr, separator, separator_len);
+          memcpy (ptr + separator_len, str_array[i], strlen (str_array[i]));
+          ptr += separator_len + strlen (str_array[i]);
+        }
+    }
   else
     string = g_strdup ("");
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]