【发布时间】:2019-02-28 12:51:04
【问题描述】:
我正在尝试绘制串行和并行执行时间的输出,但不断得到 p>
TypeError:/:'list' 和'list' 的操作数类型不受支持。
如何划分列表的内容,以免出现此错误
TypeError Traceback(最近一次调用最后一次) 在 49 50 # 计算这些时间的比率 ---> 51 ratio.append(serialTime/parallelTime) 52 53
TypeError: /: 'list' 和 'list' 的操作数类型不受支持
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 ----
【问题讨论】:
-
为什么要将一个列表除以另一个列表,您究竟想在
ratio.append(serialTime/parallelTime)中发生什么 -
我想将串行执行时间除以并行执行时间,以便根据等待时间绘制它们。我确定有更好的方法,但我被卡住了
-
你能检查一下我的解决方案吗?
标签: python