how to speed up the code??
- by kaushik
i have very huge code about 600 lines plus. cant post the whole thing here.
but a particular code snippet is taking so much time,leading to problems.
here i post that part of code please tell me what to do speed up the processing..
please suggest the part which may be the reason and measure to improve them if this small part of code is understandable.
using_data={}
def join_cost(a , b):
global using_data
#print a
#print b
save_a=[]
save_b=[]
print 1
#for i in range(len(m)):
#if str(m[i][0])==str(a):
save_a=database_index[a]
#for i in range(len(m)):
# if str(m[i][0])==str(b):
#print 'save_a',save_a
#print 'save_b',save_b
print 2
save_b=database_index[b]
using_data[save_a[0]]=save_a
s=str(save_a[1]).replace('phone','text')
s=str(s)+'.pm'
p=os.path.join("c:/begpython/wavnk/",s)
x=open(p , 'r')
print 3
for i in range(6):
x.readline()
k2='a'
j=0
o=[]
while k2 is not '':
k2=x.readline()
k2=k2.rstrip('\n')
oj=k2.split(' ')
o=o+[oj]
#print o[j]
j=j+1
#print j
#print o[2][0]
temp=long(1232332)
end_time=save_a[4]
#print end_time
k=(j-1)
for i in range(k):
diff=float(o[i][0])-float(end_time)
if diff<0:
diff=diff*(-1)
if temp>diff:
temp=diff
pm_row=i
#print pm_row
#print temp
#print o[pm_row]
#pm_row=3
q=[]
print 4
l=str(p).replace('.pm','.mcep')
z=open(l ,'r')
for i in range(pm_row):
z.readline()
k3=z.readline()
k3=k3.rstrip('\n')
q=k3.split(' ')
#print q
print 5
s=str(save_b[1]).replace('phone','text')
s=str(s)+'.pm'
p=os.path.join("c:/begpython/wavnk/",s)
x=open(p , 'r')
for i in range(6):
x.readline()
k2='a'
j=0
o=[]
while k2 is not '':
k2=x.readline()
k2=k2.rstrip('\n')
oj=k2.split(' ')
o=o+[oj]
#print o[j]
j=j+1
#print j
#print o[2][0]
temp=long(1232332)
strt_time=save_b[3]
#print strt_time
k=(j-1)
for i in range(k):
diff=float(o[i][0])-float(strt_time)
if diff<0:
diff=diff*(-1)
if temp>diff:
temp=diff
pm_row=i
#print pm_row
#print temp
#print o[pm_row]
#pm_row=3
w=[]
l=str(p).replace('.pm','.mcep')
z=open(l ,'r')
for i in range(pm_row):
z.readline()
k3=z.readline()
k3=k3.rstrip('\n')
w=k3.split(' ')
#print w
cost=0
for i in range(12):
#print q[i]
#print w[i]
h=float(q[i])-float(w[i])
cost=cost+math.pow(h,2)
j_cost=math.sqrt(cost)
#print cost
return j_cost
def target_cost(a , b):
a=(b+1)*3
b=(a+1)*2
t_cost=(a+b)*5/2
return t_cost
r1='shht:ra_77'
r2='grx_18'
g=[]
nodes=[]
nodes=nodes+[[r1]]
for i in range(len(y_in_db_format)):
g=y_in_db_format[i]
#print g
#print g[0]
g.remove(str(g[0]))
nodes=nodes+[g]
nodes=nodes+[[r2]]
print nodes
print "lenght of nodes",len(nodes)
lists=[]
#lists=lists+[r1]
for i in range(len(nodes)):
for j in range(len(nodes[i])):
lists=lists+[nodes[i][j]]
#lists=lists+[r2]
print lists
distance={}
for i in range(len(lists)):
if i==0:
distance[str(lists[i])]=0
else:
distance[str(lists[i])]=long(123231223)
#print distance
group_dist=[]
infinity=long(123232323)
for i in range(len(nodes)):
distances=[]
for j in range(len(nodes[i])):
#distances=[]
if i==0:
distances=distances+[[nodes[i][j], 0]]
else:
distances=distances+[[nodes[i][j],infinity]]
group_dist=group_dist+[distances]
#print distances
print "group_distances",group_dist
#print "check",group_dist[0][0][1]
#costs={}
#for i in range(len(lists)):
#if i==0:
# costs[str(lists[i])]=1
#else:
# costs[str(lists[i])]=get_selfcost(lists[i])
path=[]
for i in range(len(nodes)):
mini=[]
if i!=(len(nodes)-1):
#temp=long(123234324)
#Now calculate the cost between the current node and each of its neighbour
for k in range(len(nodes[(i+1)])):
for j in range(len(nodes[i])):
current=nodes[i][j]
#print "current_node",current
j_distance=join_cost( current , nodes[i+1][k])
#t_distance=target_cost( current , nodes[i+1][k])
t_distance=34
#print distance
#print "distance between current and neighbours",distance
total_distance=(.5*(float(group_dist[i][j][1])+float(j_distance))+.5*(float(t_distance)))
#print "total distance between the intial_nodes and current neighbour",total_distance
if int(group_dist[i+1][k][1]) > int(total_distance):
group_dist[i+1][k][1]=total_distance
#print "updated distance",group_dist[i+1][k][1]
a=current
#print "the neighbour",nodes[i+1][k],"updated the value",a
mini=mini+[[str(nodes[i+1][k]),a]]
print mini