gpt4 book ai didi

python - 使用嵌套的 'for' 语句加速 Python 'if' 循环

转载 作者:行者123 更新时间:2023-11-28 21:03:57 24 4
gpt4 key购买 nike

我正在尝试加快我正在运行的循环,以将数据分为两类。通常我不会太在意速度,但我发现现在这段代码的速度在多次迭代后实际上正在急剧下降。这是我编写代码的方式:

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++ 类(class),你可以创建比嵌套的“if”语句运行得更快的列表。这是正确的吗?如果是这样,我可以在 python 中执行此操作吗?我现在正在运行 python 3.5。感谢您的帮助。

最佳答案

你的问题,是一个主要时间浪费,是表单的陈述

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 个元素的列表,就地追加比复制和分配快 三千倍。如果您想衡量 yield ,您可以分析自己数据的一个子集。

关于python - 使用嵌套的 'for' 语句加速 Python 'if' 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45963548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com