![](/img/trans.png)
[英]Improving Run Time for deduping lists based on only certain columns in Python
[英]Improving run time with Python data structure
我想改善代码的运行时间。 当前速度非常慢,因为我以追加模式打开文本文件并将值写入文件的末尾,然后每次循环时都关闭文件。 有人可以帮助我将我的所有数据存储在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
代码中的大部分时间都由余弦计算占用(不是由于写入文件)。 用time
来衡量的描述你的代码的不同部分所花费的时间在这里 。
您可以通过转换为如下所示的numpy数组操作来获得显着改善(快10倍)(未经测试,请在使用前检查)
for k in range(0,n):
A=[]
wi=[]
for i in range (0,N):
wi.append(Dw/2+i*Dw) #Middle of distribution
# convert wi to numpy array as that allows array operations
wi_np = numpy.array(wi)
# Sv calculation does not change with j so moving it outside the loop
# also instead of looping over i to calculate each element
# use numpy's array operations over wi_np
p1 = (wi_np / 15)**2
p2 = p1 * 100 # => (wi_np / 1.5) ** 2
arg1 = (1 + 4 * 0.6**2 * p1) / ((1 - p1)**2 + 4 * 0.6**2 * p1)
arg2 = 0.0000753 * p2**2 / ((1 - p2)**2 + 4 * 0.6**2 * p2)
Sv_np = arg1 * arg2
# amp is an array of size N
amp = numpy.sqrt(Sv_np * Dw)
for j in range (0,tlength):
# changing the dimensions from (N, 1) to N
# so that phi is an array of scalars
# otherwise it messes up the array operations
phi = numpy.random.rand(N) * 2 * Pi
# angle is an array of size N
angle = wi_np * t[j] + phi
# numpy cos is faster than math.cos
# the multiplication operator between numpy arrays is element wise
# hence Aj_np is also array of size N
Aj_np = 2 * amp * numpy.cos(angle)
A.append(sum(Aj_np))
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.