Slow String Repeat (was: [Python-Dev] PyBuffer* vs. array.array())

Jack Jansen Jack.Jansen@oratrix.com
Mon, 6 Jan 2003 02:45:03 +0100


On maandag, jan 6, 2003, at 00:46 Europe/Amsterdam, Christian Tismer 
wrote:

> The central copying code in stringobject.c is the following
> tight loop:
>
> 	for (i = 0; i < size; i += a->ob_size)
> 		memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
>
> For my example, this memcpy is started for every single
> of the one million bytes. So the overhead of memcpy,
> let is be a function call or a macro, will be executed
> a million times.

Oops, I replied before seeing this message, this does sound plausible. 
But that gives an easy way to fix it: for copies larger than a certain 
factor just copy the source object, then duplicate the source object 
until you're at size/2, then duplicat the last bit.

That is, if it is worth the trouble to optimize this,
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma 
Goldman -