How can I speed this function up?

Peter Otten __peter__ at web.de
Sat Nov 18 13:55:45 CET 2006


Chris wrote:

> So my question is how can I speed up what's happening inside the
> function write_data()? Only allowed to use vanilla python (no psycho or
> other libraries outside of a vanilla python install).

> def write_data1(out, data):
>      for i in data:
>          if i[0] is 'ELEMENT':
>              out.write("%s %06d " % (i[0], i[1]))
>              for j in i[2]:
>                  out.write("%d " % (j))
>              out.write("\n")

# reference, modified to avoid trailing ' '
def write_data(out, data):
    for i in data:
        if i[0] == 'ELEMENT':
            out.write("%s %06d" % (i[0], i[1]))
            for j in i[2]:
                out.write(" %d" % j)
            out.write("\n")

# Norvitz/Lundh
def writelines_data(out, data, map=map, str=str):
      SPACE_JOIN = ' '.join
      out.writelines(
         "ELEMENT %06d %s\n" % (i1, SPACE_JOIN(map(str, i2)))
               for i0, i1, i2 in data if i0 == 'ELEMENT'
         )

def print_data(out, data):
    for name, index, items in data:
        if name == "ELEMENT":
            print >> out, "ELEMENT %06d" % index,
            for item in items:
                print >> out, item,
            print >> out


import time

data = []
for i in range(500000):
     data.append(("ELEMENT", i, (1,2,3,4,5,6)))

for index, write in enumerate([write_data, writelines_data, print_data]):
    fname = "test%s.txt" % index
    out = open(fname,'w')
    start = time.time()
    write(out, data)
    out.close()
    print write.__name__, time.time()-start

for fname in "test1.txt", "test2.txt":
    assert open(fname).read() == open("test0.txt").read(), fname

Output on my machine:

$ python2.5 writedata.py
write_data 10.3382301331
writelines_data 5.4960360527
print_data 3.50765490532

Moral: don't forget about good old print. It does have an opcode(*) of its
own, after all.

Peter

(*) or two



More information about the Python-list mailing list