什么是相比对象的numpy的阵列与问候SPEED列表的列表的益处/缺点? [英] What are the benefits / drawbacks of a list of lists compared to a numpy array of OBJECTS with regards to SPEED?

查看:168
本文介绍了什么是相比对象的numpy的阵列与问候SPEED列表的列表的益处/缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个跟进这个问题。

<一个href=\"http://stackoverflow.com/questions/26767694/what-are-the-benefits-drawbacks-of-a-list-of-lists-compared-to-a-numpy-array-o\">What好处/比较对象的数组numpy的与问候内存列出清单的弊端?

我有兴趣了解使用numpy的阵列VS列表的列表速度的影响时,阵列类型的对象

如果有人有兴趣,我使用对象:

 进口gmpy2通用汽车
gm.mpfr('0')#&下; - 这是在对象


解决方案

numpy的最大的通常的好处,至于速度的推移,来自能够矢量化操作,这意味着你更换围绕一个Python函数调用Python的循环与周围的一些碳循环内联C(或者甚至是自定义SIMD组装)code。可能不会有内置的 MPFR 对象的数组矢量操作,使主要优势就消失了。

不过,也有一些地方,你还是会受益:


  • ,要求在纯Python副本有些操作在numpy的,调换一个二维数组,切片一列或行,甚至重塑尺寸都是通过包装一个指向不同的跨步相同的底层数据做基本上是免费的信息。由于您最初的问题专门询问了 A.T ,是的,这基本上是免费的。

  • 很多操作可以就地更容易在numpy的比Python中执行,这可以为您节省更多的副本。

  • 即使需要副本的时候,它的速度更快批量复制的大内存数组,然后所有引用计数比通过嵌套的列表深拷贝下来,一路遍历对象。

  • 这是一个更容易编写自己的定制用Cython code进行矢量与numpy的任意操作比使用Python。

  • 您仍可以从使用​​ <$ C $得到一些好处C> np.vectorize 周围正常的Python函数,pretty大致相同的顺序从列表中COM prehension克服for语句的利益上。

  • 在一定的尺寸范围,如果你小心使用适当的跨步,numpy的可以让你优化缓存本地(或虚拟机交换,更大尺寸)相对容易,但真的没有办法做到这一点在所有列表的列表。这是要少得多,当你正在处理的指针数组,可以将分散在内存,可以直接在阵列中嵌入值打交道时不是对象一场胜利,但它仍然是东西。

至于缺点......好了,一个明显的一种是使用numpy的限制您CPython的或有时PyPy(希望,在未来的有时会变成几乎总是,但它并不完全有截至2014年);如果你的code将运行Jython或IronPython的或不NumPyPy PyPy更快,这可能是一个很好的理由,坚持使用名单。

This is a follow up to this question

What are the benefits / drawbacks of a list of lists compared to a numpy array of OBJECTS with regards to MEMORY?

I'm interested in understanding the speed implications of using a numpy array vs a list of lists when the array is of type object.

If anyone is interested in the object I'm using:

import gmpy2 as gm
gm.mpfr('0') # <-- this is the object

解决方案

The biggest usual benefits of numpy, as far as speed goes, come from being able to vectorize operations, which means you replace a Python loop around a Python function call with a C loop around some inlined C (or even custom SIMD assembly) code. There are probably no built-in vectorized operations for arrays of mpfr objects, so that main benefit vanishes.

However, there are some place you'll still benefit:

  • Some operations that would require a copy in pure Python are essentially free in numpy—transposing a 2D array, slicing a column or a row, even reshaping the dimensions are all done by wrapping a pointer to the same underlying data with different striding information. Since your initial question specifically asked about A.T, yes, this is essentially free.
  • Many operations can be performed in-place more easily in numpy than in Python, which can save you some more copies.
  • Even when a copy is needed, it's faster to bulk-copy a big array of memory and then refcount all of the objects than to iterate through nested lists deep-copying them all the way down.
  • It's a lot easier to write your own custom Cython code to vectorize an arbitrary operation with numpy than with Python.
  • You can still get some benefit from using np.vectorize around a normal Python function, pretty much on the same order as the benefit you get from a list comprehension over a for statement.
  • Within certain size ranges, if you're careful to use the appropriate striding, numpy can allow you to optimize cache locality (or VM swapping, at larger sizes) relatively easily, while there's really no way to do that at all with lists of lists. This is much less of a win when you're dealing with an array of pointers to objects that could be scattered all over memory than when dealing with values that can be embedded directly in the array, but it's still something.

As for disadvantages… well, one obvious one is that using numpy restricts you to CPython or sometimes PyPy (hopefully in the future that "sometimes" will become "almost always", but it's not quite there as of 2014); if your code would run faster in Jython or IronPython or non-NumPyPy PyPy, that could be a good reason to stick with lists.

这篇关于什么是相比对象的numpy的阵列与问候SPEED列表的列表的益处/缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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