At Friday 17/11/2006 23:40, Chris wrote:

This is just some dummy code to mimic what's being done in the real
code. The actual code is python which is used as a scripting language in
a third party app. The data structure returned by the app is more or
less like the "data" list in the code below. The test for "ELEMENT" is
necessary ... it just evaluates to true every time in this test code. In
the real app perhaps 90% of tests will also be true.

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).

I have a vested interest in showing a colleague that a python app can
yield results in a time comparable to his C-app, which he feels is mch
faster. I'd like to know what I can do within the constraints of the
python language to get the best speed possible. Hope someone can help.

If you can assume that all items have 6 numbers, it appears best to unroll the inner iteration. Below is my best attempt with ideas from other replies too, including some alternatives. The timing is only approximate and had a wide dispersion; median of three. But it's clear that the main gain comes from calling out.write only once:

Notice that you can't, in general, use i[0] is 'ELEMENT' unless you can guarantee that i[0] is an interned string (and if it comes from another process, chances are it isn't). Using intern(i[0]) is 'ELEMENT' would work, but slows down your program.

# initial: 11.66s
def write_data1(out, data):
     write = out.write
     for i in data:
         if i[0] == 'ELEMENT': # sorry but can't guarantee identity

# 6.21s
             write("ELEMENT %06d %s\n" % (i[1], "%d %d %d %d %d %d " % i[2]))

# 6.92s
#             write("ELEMENT %06d %s \n" % (i[1], " ".join(map(str,i[2]))))

# 8.30s [i]
#             i2 = i[2]
# write("ELEMENT %06d %d %d %d %d %d %d \n" % (i[1], i2[0], i2[1], i2[2], i2[3], i2[4], i2[5]))

# 7.04s __getitem__
#            i2 = i[2].__getitem__
# write("ELEMENT %06d %d %d %d %d %d %d \n" % (i[1], i2(0), i2(1), i2(2), i2(3), i2(4), i2(5)))



--
Gabriel Genellina
Softlab SRL
__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to