具有 1 亿个零的高效 Python 数组? [英] Efficient Python array with 100 million zeros?

查看:34
本文介绍了具有 1 亿个零的高效 Python 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中初始化和访问大型数组元素的有效方法是什么?

What is an efficient way to initialize and access elements of a large array in Python?

我想在 Python 中创建一个数组,其中包含 1 亿个条目,无符号 4 字节整数,初始化为零.我想要快速访问数组,最好使用连续内存.

I want to create an array in Python with 100 million entries, unsigned 4-byte integers, initialized to zero. I want fast array access, preferably with contiguous memory.

奇怪的是,NumPy 数组似乎执行得很慢.我可以尝试其他替代方法吗?

Strangely, NumPy arrays seem to be performing very slow. Are there alternatives I can try?

array.array 模块,但我没有看到方法有效地分配 1 亿个条目的块.

There is the array.array module, but I don't see a method to efficiently allocate a block of 100 million entries.

对评论的回复:

  • 我不能使用稀疏数组.对于这个算法来说太慢了,因为数组很快就会变得密集.
  • 我知道 Python 是解释型的,但肯定有一种方法可以进行快速数组操作吗?
  • 我做了一些分析,使用 NumPy 每秒获得​​大约 160K 次数组访问(按索引查找或更新元素).这看起来很慢.

推荐答案

我做了一些分析,结果完全违反直觉.对于简单的数组访问操作,numpy 和 array.array 比原生 Python 数组慢 10 倍.

I have done some profiling, and the results are completely counterintuitive. For simple array access operations, numpy and array.array are 10x slower than native Python arrays.

请注意,对于数组访问,我正在执行以下形式的操作:

Note that for array access, I am doing operations of the form:

a[i] += 1

个人资料:

  • [0] * 20000000

  • [0] * 20000000

  • 访问:2.3M/秒
  • 初始化:0.8s

numpy.zeros(shape=(20000000,), dtype=numpy.int32)

numpy.zeros(shape=(20000000,), dtype=numpy.int32)

  • 访问:160K/秒
  • 初始化:0.2s

array.array('L', [0] * 20000000)

array.array('L', [0] * 20000000)

  • 访问:175K/秒
  • 初始化:2.0s

array.array('L', (0 for i in range(20000000)))

array.array('L', (0 for i in range(20000000)))

  • 访问:175K/秒,大概是基于另一个 array.array 的配置文件
  • 初始化:6.7s

这篇关于具有 1 亿个零的高效 Python 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆