Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

python - Can this code be further optimized?

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('
')).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('
')).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`

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
def get_list(file, cmp, fout):
    ind, _ = min(enumerate(file), key=lambda x: abs(x[1] - cmp))
    return fout[ind].rstrip('
').split(' ')

root = r'c:egpythonwavnk'
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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...