[Tutor] String concatenation too slow

Shrutarshi Basu technorapture at gmail.com
Tue Jul 1 07:55:27 CEST 2008


My bad, should have included some code. Here's the function which does
the grunt work. self.axiom is a string, where each character gets
replaced by its counterpart from self.rules. output then goes back to
the calling function. That's the end of one generation of the string.
The next generation happens when generate() is called again after
axiom has been replaced (usually by output and some additions)

    def generate(self):
        output = ''
        for element in self.axiom:
            if element in self.rules.keys():
                output = output + self.rules[element]
            else:
                output = output + element
        return output

Looking at Wesley's example. the function should then yield each
character replacement (a dict lookup) and the join() would put
everything together. I think that would be faster. Will try soon.
Thanks,
Basu

On Tue, Jul 1, 2008 at 1:37 AM, wesley chun <wescpy at gmail.com> wrote:
>> I've been
>>  using string concatenation to read through the string, and then create
>>  the new one based on a dictionary lookup. However it becomes very slow
>>  once the string gets very long (several thousand characters). [...]
>> I was wondering if there might be a
>>  faster alternative to string concatenation. Would appending to a list
>>  of strings be faster?
>
> without knowing more about your application, my 1st inclination would
> be to turn your code that "appends" each successive addition to the
> string into a generator function.  then when you need to final
> massively large string, i'd use a generator expression inside the call
> to the delimiter's join() method.
>
> for example:
>
> def nextLsystem(...):
>        :
>    for n in range(XXX):
>        # blah-blah stuff in a loop
>        yield nextStr
>
> final = ''.join(x for x in nextLsystem(XXX))
>
> i like this code because it doesn't keep building up a data structure
> like continuously concatenating strings nor continually appending to a
> list, both of which are memory-intensive.
>
> i'm using a generator to create each successive string, without saving
> previous result necessarily.  then the generator expression -- unlike
> a list comprehension which must build an entire list -- passes each
> string to join(), which then creates the final string.
>
> i'm sure others have better ideas, but like it said, it's just a gut
> shot from me here.
>
> good luck!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
>    http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>



-- 
The ByteBaker :
http://www.bytebaker.com


More information about the Tutor mailing list