How can I speed this function up?

Chris cfriedl at bigpond.net.au
Fri Nov 17 22:05:35 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.
> 
> 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.
> 
> 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")
> 
> import timeit
> 
> # basic data mimicing data returned from 3rd party app
> data = []
> for i in range(500000):
>     data.append(("ELEMENT", i, (1,2,3,4,5,6)))
> 
> # write data out to file
> fname = "test2.txt"
> out = open(fname,'w')
> start= timeit.time.clock()
> write_data2(out, data)
> out.close()
> print timeit.time.clock()-start
> 
> 

with this function I went from 8.04 s to 6.61 s. Now running up against 
my limited knowledge of python. Any chance of getting faster?

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




More information about the Python-list mailing list