我只是改一个程序,我写握住我的数据,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的阵列这么快
发布评论