为什么numpy的阵列这么快

编程入门 行业动态 更新时间:2024-10-25 23:22:10
本文介绍了为什么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!

我想知道它是怎么做的。我想这就是因为它消除了对循环但除此之外,我难倒。

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:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1494281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   numpy

发布评论

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

>www.elefans.com

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