Tcl Code Refactoring, Part 2
lreplace
has to re-create another list after the replacement, it is still better than going through the whole list.
With this modification, I managed to squeeze another 0.7 second out from the run time. Ok, I think I am kind of hitting the performance limit, so what will be the next step. Since I am learning Python, may be the snake has something to offer.
Bingo! The default Python installation comes with CSV module. Hey that's a good opportunity for me to practise my Python skill. Below is a simple skeleton to read and write in CSV format.
#! /usr/bin/python import csv, sys if len(sys.argv) != 3: print "Usage:", sys.argv[0], "csv(in) csv(out)" exit(1) reader=csv.reader(open(sys.argv[1],"r")) writer=csv.writer(open(sys.argv[2],"w")) for row in reader: writer.writerow(row)
Comparing the above functionality with Tcl, Python is 6 times faster! With the characteristics of list object in Python being mutable, it is very efficient to replace values in Python list. However, in Tcl you will have to recreate another list object.
For this exercise, I will definitely go the Python way. So stay tune for more performance news. To be continued ...
Labels: performance, python, Tcl
0 Comments:
Post a Comment
<< Home