我试图加快这个循环我正在努力将数据分成两类。通常我不太关心速度,但我发现现在这个代码的速度实际上在多次迭代后显着减慢。以下是我编写代码的方法:
plane1Data = []
plane2Data = []
plane1Times = []
plane2Times = []
plane1Dets = []
plane2Dets = []
t1 = time.time()
for i in range(0,len(adcBoardVals)):#10000):
tic = time.time()
if adcBoardVals[i] == 5:
if adcChannel[i] == 0:
#detectorVal = detectorVal + [0]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [0]
elif adcChannel[i] == 1:
#detectorVal = detectorVal + [1]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [1]
elif adcChannel[i] == 2:
#detectorVal = detectorVal + [2]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [2]
elif adcChannel[i] == 3:
#detectorVal = detectorVal + [3]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [3]
elif adcChannel[i] == 4:
#detectorVal = detectorVal + [4]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
#plane1Dets = plane1Dets + [4]
elif adcChannel[i] == 5:
#detectorVal = detectorVal + [5]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [5]
elif adcChannel[i] == 6:
#detectorVal = detectorVal + [6]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [6]
elif adcChannel[i] == 7:
#detectorVal = detectorVal + [7]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [7]
elif adcBoardVals[i] == 7:
if adcChannel[i] == 0:
#detectorVal = detectorVal + [16]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [16]
elif adcChannel[i] == 1:
#detectorVal = detectorVal + [17]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [17]
elif adcChannel[i] == 2:
#detectorVal = detectorVal + [18]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [18]
elif adcChannel[i] == 3:
#detectorVal = detectorVal + [19]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [19]
elif adcChannel[i] == 4:
#detectorVal = detectorVal + [20]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [20]
elif adcChannel[i] == 5:
#detectorVal = detectorVal + [21]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [21]
elif adcChannel[i] == 6:
#detectorVal = detectorVal + [22]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [22]
elif adcChannel[i] == 7:
#detectorVal = detectorVal + [23]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [23]
elif adcBoardVals[i] == 6:
if adcChannel[i] == 0:
#detectorVal = detectorVal + [8]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [8]
elif adcChannel[i] == 1:
#detectorVal = detectorVal + [9]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [9]
elif adcChannel[i] == 2:
#detectorVal = detectorVal + [10]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [10]
elif adcChannel[i] == 3:
#detectorVal = detectorVal + [11]
plane1Data = plane1Data + [rawDataMat[i,:]]
plane1Times = plane1Times + [timeVals[i]]
plane1Dets = plane1Dets + [11]
elif adcChannel[i] == 4:
#detectorVal = detectorVal + [12]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [12]
elif adcChannel[i] == 5:
#detectorVal = detectorVal + [13]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [13]
elif adcChannel[i] == 6:
#detectorVal = detectorVal + [14]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [14]
elif adcChannel[i] == 7:
#detectorVal = detectorVal + [15]
plane2Data = plane2Data + [rawDataMat[i,:]]
plane2Times = plane2Times + [timeVals[i]]
plane2Dets = plane2Dets + [15]
if i%100000 == 0:
print('k = ',i)
toc = time.time()
print('tictoc = ',toc-tic)
print('elapsed = ',toc-t1)
elif i>900000:
if i%1000 == 0:
print('k = ',i)
toc = time.time()
print('tictoc = ',toc-tic)
print('elapsed = ',toc-t1)
#detectorVal = np.array(detectorVal,dtype='float')
plane1Data = np.array(plane1Data,dtype='float')
plane2Data = np.array(plane2Data,dtype='float')
plane1Times = np.array(plane1Times,dtype='float')
plane2Times = np.array(plane2Times,dtype='float')
plane1Dets = np.array(plane1Dets,dtype='int')
plane2Dets = np.array(plane2Dets,dtype='int')
我依旧记得从一段时间的c ++课程开始,你可以创建比嵌套更快的列表,如果'声明。这是正确的,如果是这样,我可以在python中这样做吗?我现在正在运行python 3.5。谢谢你的帮助。
答案 0 :(得分:6)
你的问题,这是主要时间浪费,是表格的陈述
list_variable = list_variable + [ new_value ]
在每次循环迭代中调用其中三个,例如:
plane1Data = plane1Data + [rawDataMat[i,:]]
因为您可能对list_variable
指向的列表有其他引用,所以Python会在每次调用时构造一个完整的列表副本,只在执行赋值时丢弃原始列表。对所有列表扩展使用以下表单,您将看到天文数据改进:
list_variable += [ new_value ]
以下是这种情况的证据:
>>> from timeit import timeit
>>> x=list(range(100000))
>>> timeit("x += [99]", "from __main__ import x", number=1000)
0.00023529794998466969
>>> x=list(range(100000))
>>> timeit("x = x + [99]", "from __main__ import x", number=1000)
0.7576854809885845
>>> 0.7576854809885845 / 0.00023529794998466969
3220.110846855868
你有它。对于这个100000个元素的列表,附加到位的速度比复制和分配快 三千次 。如果您想衡量收益,可以对自己数据的子集进行概要分析。