Why it is so dramatical?

holger krekel pyth at devel.trillke.net
Mon Sep 2 07:23:48 EDT 2002


Bo M. Maryniuck wrote:
> On Monday 02 September 2002 12:45, holger krekel wrote:
> > It's much more interesting to look at real performance limitations
> > instead of some theoretical pusing-the-limits loops.
> Hmmm... I've raised this, `coz I have a problem with a perfomance in Python 
> app, which generates XML for XSLT. I found, that 
> 
> 	string1 += string2
> 
> ...sucks and just for experiment I got rewrote XML generating piece of code to 
> Perl and I've found, that Perl does the same *much* faster than Python. :( 

Did you try using cStringIO? With your first example

[hpk at cobra /tmp]$ python slow.py
<function func1 at 0x80fda84> took 4.68154907227 seconds
<function func2 at 0x810be84> took 0.00871503353119 seconds

where func2 is:

def func2():
    _body = cStringIO.StringIO()
    _data = 'A'*81
    for a in xrange(0, 0xfff):
        _body.write(_data)
    return _body.getvalue()

(you need to do 'import cStringIO' at the top of the file.)
and func1 is your code.

regards,

    holger


P.S: the full script

import cStringIO

_data = 'A'*81

def func1():
    _body = ''
    _data = 'A'*81

    for a in xrange(0, 0xfff):
        _body += _data
    return _body

def func2():
    _body = cStringIO.StringIO()
    _data = 'A'*81
    for a in xrange(0, 0xfff):
        _body.write(_data)
    return _body.getvalue()

def drive(func):
    import time
    start = time.time()
    res = func()
    end = time.time()
    print func, "took",end-start,"seconds"
    return res


print len(drive(func1))
print len(drive(func2))





More information about the Python-list mailing list