How can I speed this function up?
nnorwitz at gmail.com
nnorwitz at gmail.com
Sat Nov 18 05:20:46 EST 2006
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.
As others have said, without info about what's happening in C, there's
no way to know what's equivalent or fast enough.
> 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).
Generally, don't create objects, don't perform repeated operations. In
this case, batch up I/O.
> 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")
def write_data1(out, data, map=map, str=str):
SPACE_JOIN = ' '.join
lines = [("ELEMENT %06d " % i1) + SPACE_JOIN(map(str, i2))
for i0, i1, i2 in data if i0 == 'ELEMENT']
out.write('\n'.join(lines))
While perhaps a bit obfuscated, it's a bit faster than the original.
Part of what makes this hard to read is the crappy variable names. I
didn't know what to call them. This version assumes that data will
always be a sequence of 3-element items.
The original version took about 11.5 seconds, the version above takes
just over 5 seconds.
YMMV,
n
More information about the Python-list
mailing list