Re: g_strjoinv() is very slow

On Sun, Sep 24, 2000 at 03:48:19PM -0400, Sean Middleditch wrote:
> >     OK. Thanks for all your comments.
> >  I've attached a new version which takes all your remarks in account.
> >
> OK, just to point something out, all those for (int i = 1; array[i] != NULL;
> i++) and whatnot's in the for loops should be something like
> for (char *c = str + 1; *c != 0; c ++)
> That way, you don't have to do an index lookup on every iteration... it'll
> speed the code up a lot (one less multiplication per iteration).

I used to think so too.  But have you done profiling on recent machines?  In
fact the index is faster.  I would say it has to do with good optimization
and possibility to do larger prefetch.  With the pointer you can really only
fetch the value at the dereference time and then have to refetch it.
Try profiling this, it's quite amazing.

So, no, those shouldn't be changed.  I use the pointer iteration a lot
because I find it nicer sometimes, but the array index just IS faster.

> That's how functions like these are coded in the various incarnations of
> libc, too.  Never use an index value to an array when a pointer to the value
> is usable.  If you need to find the actual index value from the start of an
> array, you can use (c - str), which is one subtraction per loop, which is
> still faster than a multiplication.

But you kill nice cache behaviour.  Operations like mutliplication etc... are
by now fast enough that the bottleneck is the memory fetch.


George <jirka 5z com>
   If God did not exist, it would be necessary to invent him.
                       -- Voltaire

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