本文介绍了程序效率低下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我编写了以下简单的程序来循环浏览我们的帮助文件 并修复一些错误(如果你看不到微妙的RE搜索' 正在发生,我们正在用_''替换书签中的空格。 除了一件事,程序运行得很好。通过搜索中的后期文件,然后通过早期的 ,这显然会慢得多b $ b ......在有人批评之前,我认识到那个中间部分 可以通过for循环简化......我只是没有清理它 up ... 问题是前300个文件需要大约10-15秒,而最后300个文件需要大约2分钟才能获得 ...如果我们在一次运行中执行的文件超过1500个/ b $ b文件,它只是挂断而且永远不会结束...... 这里有解决方案吗?我在做什么呢? 低效? #文件:masseditor.py import re 导入os 导入时间 def massreplace(): editfile = open(" pathname \ editfile.txt") filestring = editfile.read() filelist = filestring.splitlines() ## errorcheck = re.compile(''(a name =)+(。*)( - )+(。*)(>< / a>)+'') for i in range( len(filelist)): source = open(filelist [i]) starttext = source.read() interimtext = replacecycle( starttext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = repla cecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) interimtext = replacecycle(interimtext) finaltext = replacecycle(interimtext) source.close() source = open(filelist [i]," w") source.write(finaltext) source.close() ## if errorcheck.findall(finaltext)!= []: ## print errorcheck.findall(finaltext) ## print filelist [i] if i == 100: print" ;完成100" 打印time.clock() elif i == 300: 打印完成300 print time.clock() elif i == 600: print" done 600" print time.clock () elif i == 1000: print" done 1000" print time.clock() 打印完成 打印i 打印time.clock() def replacecycle(starttext): p1 = re.compile(''(href = | HREF =)+(。*)(#)+(。*)()+(。*) (">)+'') p2 = re.compile(''(name =")+(。*)()+(。*)(">) +'') p3 = re.compile(''(href = | HREF =)+(。*)(#)+(。*)(\'')+(。* )("> )+'') p4 = re.compile(''(name =")+(。*)(\'')+(。*)(">)+ '') p5 = re.compile(''(href = | HREF =)+(。*)(#)+(。*)( - )+(。*)(" >)+'') p6 = re.compile(''(name =")+(。*)( - )+(。*)(">)+' ') p7 = re.compile(''(href = | HREF =)+(。*)(#)+(。*)(<)+(。*)(" >)+'') p8 = re.compile(''(name =")+(。*)(<)+(。*)(">)+ '') p7 = re.compile(''(href = | HREF =")+(。*)(#)+(。*)(:) +(。*)( ">)+'') p8 = re.compile(''(name =")+(。*)(:) +(。*)(">) +'') p9 = re.compile(''(href = | HREF =")+(。*)(#)+(。*)(\?)+(。 *)(">)+'') p10 = re.compile(''(name =")+(。*)(\?)+(。*)( ">)+'') p100 = re.compile(''(a name =)+(。*)( - )+(。*)(>< / a> ;)+'') q1 = r" \\\\\\\\\\\& q2 = r" \\ \\1\2_\4 \\ 5" interimtext = p1.sub(q1,starttext) interimtext = p2.sub(q2,interimtext) interimtext = p3 .sub(q1,interimtext) interimtext = p4.sub(q2,interimtext) interimtext = p5.sub(q1,interimtext) interimtext = p6.sub(q2,interimtext) interimtext = p7.sub(q1,interimtext) interimtext = p8.sub(q2,interimtext) interimtext = p9.sub(q1,interimtext) interimtext = p10.sub(q2,interimtext) interimtext = p100.sub(q2,interimtext ) 返回interimtext massreplace()I wrote the following simple program to loop through our help filesand fix some errors (in case you can''t see the subtle RE search that''shappening, we''re replacing spaces in bookmarks with _''s)the program works great except for one thing. It''s significantlyslower through the later files in the search then through the earlyones... Before anyone criticizes, I recognize that that middle sectioncould be simplified with a for loop... I just haven''t cleaned itup...The problem is that the first 300 files take about 10-15 seconds andthe last 300 take about 2 minutes... If we do more than about 1500files in one run, it just hangs up and never finishes...Is there a solution here that I''m missing? What am I doing that is soinefficient?# File: masseditor.pyimport reimport osimport timedef massreplace():editfile = open("pathname\editfile.txt")filestring = editfile.read()filelist = filestring.splitlines()## errorcheck = re.compile(''(a name=)+(.*)(-)+(.*)(></a>)+'')for i in range(len(filelist)):source = open(filelist[i])starttext = source.read()interimtext = replacecycle(starttext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)interimtext = replacecycle(interimtext)finaltext = replacecycle(interimtext)source.close()source = open(filelist[i],"w")source.write(finaltext)source.close()## if errorcheck.findall(finaltext)!=[]:## print errorcheck.findall(finaltext)## print filelist[i]if i == 100:print "done 100"print time.clock()elif i == 300:print "done 300"print time.clock()elif i == 600:print "done 600"print time.clock()elif i == 1000:print "done 1000"print time.clock()print "done"print iprint time.clock()def replacecycle(starttext):p1= re.compile(''(href=|HREF=)+(.*)(#)+(.*)( )+(.*)(">)+'')p2= re.compile(''(name=")+(.*)( )+(.*)(">)+'')p3= re.compile(''(href=|HREF=)+(.*)(#)+(.*)(\'')+(.*)("> )+'')p4= re.compile(''(name=")+(.*)(\'')+(.*)(">)+'')p5= re.compile(''(href=|HREF=)+(.*)(#)+(.*)(-)+(.*)(">)+'')p6= re.compile(''(name=")+(.*)(-)+(.*)(">)+'')p7= re.compile(''(href=|HREF=)+(.*)(#)+(.*)(<)+(.*)(">) +'')p8= re.compile(''(name=")+(.*)(<)+(.*)(">)+'')p7= re.compile(''(href=|HREF=")+(.*)(#)+(.*)(:)+(.*)("> )+'')p8= re.compile(''(name=")+(.*)(:)+(.*)(">)+'')p9= re.compile(''(href=|HREF=")+(.*)(#)+(.*)(\?)+(.*)(" >)+'')p10= re.compile(''(name=")+(.*)(\?)+(.*)(">)+'')p100= re.compile(''(a name=)+(.*)(-)+(.*)(></a>)+'')q1= r"\1\2\3\4_\6\7"q2= r"\1\2_\4\5"interimtext = p1.sub(q1, starttext)interimtext = p2.sub(q2, interimtext)interimtext = p3.sub(q1, interimtext)interimtext = p4.sub(q2, interimtext)interimtext = p5.sub(q1, interimtext)interimtext = p6.sub(q2, interimtext)interimtext = p7.sub(q1, interimtext)interimtext = p8.sub(q2, interimtext)interimtext = p9.sub(q1, interimtext)interimtext = p10.sub(q2, interimtext)interimtext = p100.sub(q2, interimtext)return interimtextmassreplace()推荐答案 我唯一看到的是你在调用replacecycle()时编译所有RE的每个 时间。他们真的只需要编译一次,但我不知道为什么会导致 渐进式减速。 FWIW,在我看来像shell + sed脚本将是问题的明显解决方案。The only thing I see is that you compile all of the RE''s everytime you call replacecycle(). They really only need to becompiled once, but I don''t know why that would cause theprogressive slowing.FWIW, it seems to me like a shell+sed script would be theobvious solution to the problem. - 格兰特爱德华兹格兰特哇!性感活跃的你还是 吗?你有没有看到加强版?--Grant Edwards grante Yow! Are you stillat SEXUALLY ACTIVE? Did youvisi.com BRING th'' REINFORCEMENTS? 优化的一个显而易见的机会就是编译那些只有 一次在程序开头而不是每次 replacecycle()被调用(每个 文件莫名其妙地被称为13次)。 - Carsten Haese http://informixdb.sourceforge.net 这篇关于程序效率低下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 08:32