首先,感谢您的关注。我的问题是如何减少代码的执行时间。
以下是相关代码。下面的代码是在迭代中从main调用的。
def call_prism(prism_input_file,random_length):
prism_output_file = "path.txt"
cmd = "prism %s -simpath %d %s" % (prism_input_file,random_length,prism_output_file)
p = os.popen(cmd)
p.close()
return prism_output_file
def main(prism_input_file, number_of_strings):
...
for n in range(number_of_strings):
prism_output_file = call_prism(prism_input_file,z[n])
...
return当我分析我的代码时,我使用了来自“配置文件统计浏览器”的统计数据。"file close“系统命令占用的时间最长(14.546秒)。call_prism例程被调用10次。但是number_of_strings通常以数千为单位,所以我的程序需要很多时间才能完成。
如果你需要更多信息,请告诉我。顺便说一句,我也尝试使用子进程。谢谢。
发布于 2012-11-30 23:03:45
感谢您对我的问题的反馈。基于其他人提供的注释,我对我的代码做了一个并行版本,代码的性能确实得到了提高。以下是并行版本的代码片段。欢迎您的反馈,如果有的话。
def call_prism(prism_input_file,random_length):
...
cmd = "prism %s -simpath %d stdout" % (prism_input_file,random_length)
args = shlex.split(cmd)
p = subprocess.Popen(args,stdout=subprocess.PIPE)
p.poll()
prism_output_lines = p.stdout.readlines()
...
return ...
def call_prism_star(prism_input_file_random_length):
return call_prism(*prism_input_file_random_length)
def main(prism_input_file, number_of_strings,number_of_threads):
pool = Pool(processes=number_of_threads)
for n in range(0,number_of_strings,number_of_threads):
...
for i in range(number_of_threads):
a_args.append(...)
output = pool.map(call_prism_star,itertools.izip(itertools.repeat(prism_input_file),a_args))
...
returnhttps://stackoverflow.com/questions/13569127
复制相似问题