【问题标题】:Solving type error issue with python list使用 python 列表解决类型错误问题
【发布时间】: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


【解决方案1】:

除了划分两个列表之外,您可以执行以下操作来获取两个列表的比率:

ratio.append([x / y for x, y in zip(serialTime, parallelTime)])

【讨论】:

  • 我现在得到一个不同的错误 - TypeError: unsupported operand type(s) for /: 'function' and 'NoneType'
  • 可以分享列表中的项目serialTimeparallelTime
  • 我在原帖里加了
  • 你能打印出像print(serialTime)print(parallelTime)这样的东西吗
  • 不会打印出执行时间
【解决方案2】:

串行执行时间除以并行执行时间

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]

检查一下

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多