Wednesday, June 25, 2008

Optimising My First Python Program

After My First Python Program, I realised that Python provides iterable object built-in function that is optimised in execution speed, memory utilisation and coding effort.

With iterable object, you can get the object to work directly with the "for" loop that has been optimised in C code. As for the "while" loop, it is actually done in Python byte code through the Python virtual machine and generally is slower than the "for" loop. In general, one should optimise code as this:

  • change "while" loop to "for" loop
  • iterate every item in the Object using the iterable nature of the object
    • file object: for line in open("access.log","r"):
    • Dictionary object: for key in dictObj:
      instead of
      for key in dictObj.keys():
    • String object: for char in strObj:
    • List object: for item in listObj:

My previous not optimised code:

file=open(sys.argv[1],'r')
line=file.readline()
while line:
        fields=line.split()
        times=fields[1].split(':')
        hour=times[0]
        sc[hour] += int(fields[18])
        cs[hour] += int(fields[19])
        cnt[hour] += 1
        line=file.readline()
file.close()

Optimised code:

for line in open(sys.argv[1],'r'):
        fields=line.split()
        times=fields[1].split(':')
        hour=times[0]
        sc[hour] += int(fields[18])
        cs[hour] += int(fields[19])
        cnt[hour] += 1

As mentioned in Learning Python, 3rd Edition ,

The concept of “iterable objects” is relatively new in Python. It’s essentially a generalization of the notion of sequences—an object is considered iterable if it is either a physically stored sequence, or an object that produces one result at a time in the context an iteration tool like a for loop

Labels: ,

0 Comments:

Post a Comment

<< Home