【问题标题】:Dask lowest than vanilla python? What is what I'm doing wrong?Dask 比香草蟒最低?我做错了什么?
【发布时间】:2019-12-13 23:47:36
【问题描述】:

我正在测试 dask,但我无法理解 dask 比普通 python 慢得多。我是在 jupyer 中开发了两个示例来获得每个示例的时间,我认为我做错了什么

第一次使用 dask:28.5 秒,之后使用普通 python 140 毫秒

    import dask
    import dask.array as da
    %%time
    def inc(x):
        return x + 1

    def double(x):
        return x + 2

    def add(x, y):
        return x + y

    N = 100000

    data = [0 for x in range(N)]
    x = da.from_array(data, chunks=(1000))

    output = []
    for x in data:
        a = dask.delayed(inc)(x)
        b = dask.delayed(double)(x)
        c = dask.delayed(add)(a, b)
        output.append(c)

    total = dask.delayed(sum)(output)
    total.compute()
**28.8 seconds**

现在使用普通 python

    %%time
    def inc(x):
        return x + 1

    def double(x):
        return x + 2

    def add(x, y):
        return x + y

    N = 100000

    data = [0 for x in range(N)]

    output = []
    for x in data:
        a = inc(x)
        b = double(x)
        c = add(a, b)
        output.append(c)

    total = sum(output)
**140 milliseconds**

【问题讨论】:

    标签: python-3.x dask dask-delayed


    【解决方案1】:

    你的代码在我的机器上运行:38s。 这段代码:

    x = da.from_array(data, chunks=(1000))
    %time ((x + 1) + (2*x)).compute()
    

    在 24 毫秒内运行。

    x = np.array(data)
    %time ((x + 1) + (2*x))
    

    运行时间为 350us。

    积分:

    • 您的数据是否容易放入内存(numpy 或 pandas),您可能无法从 dask 获得任何信息,因为这些库已经很快
    • Dask 有数组等集合 API,所以使用它们
    • 不要for-遍历数组!
    • 如果单个函数的运行时间 sleep 来模拟 CPU 工作,因此您实际上获得了一些并行性
    • 不要多次调用.compute(),尝试将你想做的事情整合到一个对compute的调用中,它需要任意数量的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 2011-06-01
      • 1970-01-01
      • 2016-08-23
      • 2015-11-24
      • 2021-04-14
      • 2021-04-19
      • 2017-03-13
      相关资源
      最近更新 更多