为什么NumPy数组这么快?

编程入门 行业动态 更新时间:2024-10-26 01:30:26
本文介绍了为什么NumPy数组这么快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我遇到性能问题时,我刚刚更改了正在编写的程序,以将数据保存为numpy数组,而两者之间的区别令人难以置信.最初耗时30分钟,而现在耗时2.5秒!

I just changed a program I am writing to hold my data as numpy arrays as I was having performance issues, and the difference was incredible. It originally took 30 minutes to run and now takes 2.5 seconds!

我想知道它是如何做到的.我认为这是因为它消除了对for循环的需要,但除此之外,我感到很困惑.

I was wondering how it does it. I assume it is that the because it removes the need for for loops but beyond that I am stumped.

推荐答案

Numpy数组是均质类型的密集排列的数组.相反,Python列表是指向对象的指针数组,即使它们都属于同一类型.因此,您可以获得参考位置的优势.

Numpy arrays are densely packed arrays of homogeneous type. Python lists, by contrast, are arrays of pointers to objects, even when all of them are of the same type. So, you get the benefits of locality of reference.

此外,许多Numpy操作都用C实现,从而避免了Python中循环,指针定向和逐元素动态类型检查的一般开销.速度的提高取决于您执行的操作,但是在数字运算程序中,几个数量级的情况并不少见.

Also, many Numpy operations are implemented in C, avoiding the general cost of loops in Python, pointer indirection and per-element dynamic type checking. The speed boost depends on which operations you're performing, but a few orders of magnitude isn't uncommon in number crunching programs.

更多推荐

为什么NumPy数组这么快?

本文发布于:2023-10-15 12:01:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1494280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   NumPy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!