【发布时间】:2017-05-03 12:37:13
【问题描述】:
我想改进我的代码的运行时间。它目前非常慢,因为我以附加模式打开一个文本文件并将值写入文件末尾,然后每次循环时关闭文件。有人可以帮我将所有数据存储在 python 数据结构中,然后以相同的格式输出结果(即文本文件中的所有结果,每个值用空格分隔)?我是 python 新手,并不真正了解如何实现这一点。这是我的代码:
######import required packages########
import numpy
import math
#######Set working directory##########
import os
os.chdir('/Users/DevEnv/')
######Remove files generated from previous simulations#####
try:
os.remove('excitations.txt')
except OSError:
pass
##############Set Model Parameters#####################
n=2 #number of iterations -Change for desired number of loops
tlength=1501 #Length of time interval - DO NOT CHANGE
wu=100 #DO NOT CHANGE
N=250 #wu*T/4Pi approximately - DO NOT CHANGE
Pi=math.radians(180)
t=numpy.linspace(0,10,tlength)
Dw=wu/float(N)
for k in range(0,n):
A=[]
wi=[]
for i in range (0,N):
wi.append(Dw/2+i*Dw) #Middle of distribution
for j in range (0,tlength):
Aj=[]
phi=numpy.random.rand(N,1)*2*Pi #Generate random phase angle on 0,2pi
for i in range (0,N):
w=wi[i]
Sv=(((1+4*0.6**2*(w/15)**2)/((1-(w/15)**2)**2+4*0.6**2*(w/15)**2))*(0.0000753*(w/1.5)**4/((1-(w/1.5)**2)**2+4*0.6**2*(w/1.5)**2)))
Aj.append(math.sqrt(Sv*Dw)*2*math.cos(wi[i]*t[j]+phi[i]))
A.append(sum(Aj))
outFile = open('excitations.txt','a') #open/create the output file
for item in A:
outFile.write('%s ' %item)
outFile.write('\n')
outFile.close() #close the output file
【问题讨论】:
-
首先你可以存储不变的计算结果。例如- 4*0.6**2。其次,您有三个嵌套循环,这通常很糟糕。检查你是否真的需要三个?
-
感谢您的建议。我将常量值存储为变量。但是,我确实需要三个嵌套循环