Tips for accelarating python programs
1.Use join instead of + to concentrate strings.
In python, string is unchangable. Each time you add a segment to existed string, it will re-create memeory to save new string. Thus, old string will be copy-paste for many times (No. of seg – 1).
2.Use local variable
#list = [] #for i in xrange(100): # list.append(i) # list = [] append = list.append for i in xrange(100): append(i) #or list = [i for i in xrange(400)]
3.Use array from stdlib array instead of list when dealing with homogeneous data (all same type). This would save some memory.
from array import array list = array() append = list.append for i in xrange(100): append(i)
4.