Python / C: small runtime difference?

Alan Gauld alan.gauld at btinternet.com
Tue Sep 9 13:12:43 EDT 2003


On Tue, 9 Sep 2003 15:54:41 +0200, "Martin Schneider"
<martin.schneider at illusion-factory.de> wrote:

> for i in range(len(fileList)-1):
>  for j in range(len(fileList)-i-1):
>   if fileList[j+1] < fileList[j]:
>    tmp = fileList[j]
>    fileList[j] = fileList[j+1]
>    fileList[j+1] = tmp

I think you might tweak that by doing:

fileList[j],fileList[j+1] = fileList[j+1],fileList[j]

which misses out the intermediate tmp assignment.
If you are gonna tweak the C you might as well tweak the 
Python too! (But not with a real python, they tweak back 
hard! :-)

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Python-list mailing list