tags:

views:

157

answers:

2

i understand that the code given below will not be compltely understood unless i explain my whole of previous and next lines of code. But this is part of the code which is causing so much of delay in my project and want to optimize this. i want to know which code part is faulty and how could this be replaced. i guess,few can say that use of this function is heavy compared and other ligher method are available to do this work

please help,

thanks in advance

for i in range(len(lists)):
    save=database_index[lists[i]]
    #print save
    #if save[1]!='text0194'and save[1]!='text0526':
    using_data[save[0]]=save
    p=os.path.join("c:/begpython/wavnk/",str(str(str(save[1]).replace('phone','text'))+'.pm'))
    x1=open(p , 'r')
    x2=open(p ,'r')
    for i in range(6):
        x1.readline()
        x2.readline()
    gen = (float(line.partition(' ')[0]) for line in x1)
    r= min(enumerate(gen), key=lambda x: abs(x[1] - float(save[4])))
    #print r[0]
    a1=linecache.getline(str(str(p).replace('.pm','.mcep')), (r[0]+1))
    #print a1
    p1=str(str(a1).rstrip('\n')).split(' ')
    #print p1
    join_cost_index_end[save[0]]=p1
    #print join_cost_index_end

    gen = (float(line.partition(' ')[0]) for line in x2)
    r= min(enumerate(gen), key=lambda x: abs(x[1] - float(save[3])))
    #print r[0]
    a2=linecache.getline(str(str(p).replace('.pm','.mcep')), (r[0]+1))
    #print a2
    p2=str(str(a2).rstrip('\n')).split(' ')
    #print p2
    join_cost_index_strt[save[0]]=p2
    #print join_cost_index_strt
    j=j+1

    #print j
    #print join_cost_index_end
    #print join_cost_index_strt
    enter code here

here my database_index has about 2,50,000 entries`

+1  A: 
x1=open(p , 'r')
x2=open(p ,'r')

Why open the same file twice? Are you expecting it to change?

S.Lott
+3  A: 
def get_list(file, cmp, fout):
    ind, _ = min(enumerate(file), key=lambda x: abs(x[1] - cmp))
    return fout[ind].rstrip('\n').split(' ')

root = r'c:\begpython\wavnk'
header = 6
for lst in lists:
    save = database_index[lst]
    index, base, _, abs2, abs1, *_ = save
    using_data[index] = save

    base = os.path.join(root, base.replace('phone', 'text'))
    fin, fout = base + '.pm', base + '.mcep'
    file = open(fin)
    fout = open(fout).readlines()
    [next(file) for _ in range(header)]
    file = [float(line.partition(' ')[0]) for line in file]
    join_cost_index_end[index] = get_list(file, float(abs1), fout)
    join_cost_index_strt[index] = get_list(file, float(abs2), fout)

Don't:

  1. convert string to string multiple times, it'll remain a string
  2. convert value within loop when it could be done outside the loop
  3. use single-letter for meaning variables
  4. iterate over sequences with range(len(sequence))
  5. copy-paste bits of code: use functions
  6. use any code without reading docs first
  7. rely on SO for psychic debugging.
SilentGhost
Thank you! wil check this out!u inspired!!
kaki
will iteration over sequences with range(len(sequence)) slow down the execution i used similar code in whole of my project
kaki
@kaki: sure, perhaps not significantly, but it will. More importantly, it's not pythonic.
SilentGhost
@kaki: "i used similar code in whole of my project". Don't copy and paste code which is bad in the whole of your project. range(len(sequence)) is simply bad. Try not to do it.
S.Lott