[XML-SIG] speed question re DOM parsing

Juergen Hermann Juergen Hermann" <jhe@webde-ag.de
Fri, 23 Jun 2000 12:12:08 +0200


On Thu, 22 Jun 2000 19:41:53 -0700, Greg Stein wrote:

>Exactly. Bjorn solved this with StringIO. A timing comparison against
>string.join is an important test before using either approach.

The two runs I gave it (on Win/NT)...

Length of testtext is 1292
              adding    39.687
              format    189.71
                join    47.034
           chararray    67.323
            stringio    33.011

Length of testtext is 1292
              adding    40.573
              format    191.327
                join    47.09
           chararray    65.256
            stringio    32.65

The result is obvious, and also what I expected.

---%<----------------------------------
# Timings on char-wise string growing

import time, string, sys, cStringIO, array

testtext = open(sys.argv[0], "rt").read()

def timing(f, n, a):
    print "%20s" % (f.__name__,),
    r = range(n)
    t1 = time.clock()
    for i in r:
        f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a)
    t2 = time.clock()
    print "\t", round(t2-t1, 3)

def adding(x):
    result = ""
    for ch in testtext:
        result = result + ch
    #print "adding(): len =", len(result)

def format(x):
    result = ""
    for ch in testtext:
        result = "%s%s" % (result, ch)
    #print "format(): len =", len(result)

def join(x):
    chars = []
    for ch in testtext:
        chars.append(ch)
    result = string.join(chars, '')
    #print "format(): len =", len(result)

def chararray(x):
    chars = array.array("c")
    for ch in testtext:
        chars.append(ch)
    result = chars.tostring()
    #print "format(): len =", len(result)

def stringio(x):
    chars = cStringIO.StringIO()
    for ch in testtext:
        chars.write(ch)
    result = chars.getvalue()
    #print "stringio(): len =", len()


print "Length of testtext is", len(testtext)

n=1000
timing(adding, n, None)
timing(format, n, None)
timing(join, n, None)
timing(chararray, n, None)
timing(stringio, n, None)