我正在尝试绘制串行和并行执行时间的输出,但是不断得到
TypeError:/:“列表”和“列表”不受支持的操作数类型。
如何划分列表内容,以免出现此错误
TypeError跟踪(最近一次通话) 在 49 50#计算这些时间的比率 ---> 51 ratio.append(serialTime / parallelTime) 52 53
TypeError:/:“列表”和“列表”的不受支持的操作数类型
import numpy as np
import multiprocessing as mp
import time
import matplotlib.pyplot as plt
# Sleep for t seconds
def burnTime(t):
time.sleep(t)
# Main
if __name__ == '__main__':
N = 16 # The number of jobs
P = 4 # The number of processes
# A thread pool of P processes
pool = mp.Pool(P)
# Use a variety of wait times
ratio = []
wait_time = [1, 2, 3, 4, 5, 6, 7, 8]
#serial results
print("Serial Execution Times")
for _ in range(N):
start_time = time.time()
serialTime = [burnTime(t) for t in wait_time]
print("---- {} seconds ---- ".format(time.time() - start_time))
print("\n")
#parallel results
print("Parallel Execution Times")
for _ in range(N):
start_time = time.time()
parallelTime = pool.map(burnTime, wait_time)
print("---- {} seconds ---- ".format(time.time() - start_time))
for t in wait_time:
pass
# Compute jobs serially and in parallel
# Use time.time() to compute the elapsed time for each
# serialTime = 1
#parallelTime = 1
# Compute the ratio of these times
ratio.append(serialTime/parallelTime)
# Plot the results
plt.plot(wait_time, ratio, '-ob')
plt.xscale('log')
plt.xlabel('Wait Time (sec)')
plt.ylabel('Serial Time (sec) / Parallel Time (sec)')
plt.title('Speedup versus function time')
plt.show()
Output of SerialTime
---- 6.604194641113281e-05 seconds ----
---- 1.9073486328125e-06 seconds ----
---- 9.5367431640625e-07 seconds ----
---- 2.1457672119140625e-06 seconds ----
---- 2.1457672119140625e-06 seconds ----
---- 1.9073486328125e-06 seconds ----
---- 9.5367431640625e-07 seconds ----
---- 2.1457672119140625e-06 seconds ----
---- 1.9073486328125e-06 seconds ----
---- 9.5367431640625e-07 seconds ----
---- 1.1920928955078125e-06 seconds ----
---- 2.1457672119140625e-06 seconds ----
---- 1.9073486328125e-06 seconds ----
---- 1.1920928955078125e-06 seconds ----
---- 2.1457672119140625e-06 seconds ----
---- 1.9073486328125e-06 seconds ----
output of ParalleTime
---- 12.006800889968872 seconds ----
---- 12.004575967788696 seconds ----
---- 12.00544810295105 seconds ----
---- 12.006248950958252 seconds ----
---- 12.003881216049194 seconds ----
---- 12.005573749542236 seconds ----
---- 12.003982067108154 seconds ----
---- 12.003642082214355 seconds ----
---- 12.004355192184448 seconds ----
---- 12.00377106666565 seconds ----
---- 12.003960132598877 seconds ----
---- 12.004532098770142 seconds ----
---- 12.003446578979492 seconds ----
---- 12.003416776657104 seconds ----
---- 12.004649877548218 seconds ----
---- 12.004476070404053 seconds ----
答案 0 :(得分:1)
您可以执行以下操作来获得两个列表的比率,而不是划分两个列表:
ratio.append([x / y for x, y in zip(serialTime, parallelTime)])
答案 1 :(得分:0)
将串行执行时间除以并行执行时间
result = list(map(lambda x: x[0]/x[1], zip(serialTime, parallelTime)))
顺便说一句,我已经尝试了您的代码并得到:
>>> serialTime
[<function burnTime at 0x103221e18>, True, [1, 2, 3, 4, 5, 6, 7, 8]]
>>> parallelTime
[None, None, None, None, None, None, None, None]
检查