You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Numba并行循环比普通Python循环更慢?附测试案例

Python循环与Numba并行版本的性能对比

普通Python循环实现

def two_d_array_reduction_prod(n):
    shp = (13, 17)
    result1 = 2 * np.ones(shp, np.int_)
    tmp = 2 * np.ones_like(result1)

    for i in range(n):
        result1 *= tmp

    return result1

调用方式:

two_d_array_reduction_prod(50000)

耗时约 0.7482060070033185 秒。

Numba并行优化版本

@nb.njit(parallel=True)
def two_d_array_reduction_prod(n):
    shp = (13, 17)
    result1 = 2 * np.ones(shp, np.int_)
    tmp = 2 * np.ones_like(result1)

    for i in nb.prange(n):
        result1 *= tmp

    return result1

调用方式:

two_d_array_reduction_prod(50000)

耗时 3.9858204890042543 秒。

运行环境

  • Amazon Linux 2 操作系统,x86_64架构处理器
  • 8核CPU
  • 32GB内存

内容的提问来源于stack exchange,提问作者Krishna Sunuwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 00:25:59