为什么比衬里慢numpy.power 60倍? [英] Why is numpy.power 60x slower than in-lining?

查看:258
本文介绍了为什么比衬里慢numpy.power 60倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在做一些奇怪的,但使用时numpy的发现也许令人惊讶的性能损失,似乎一致不管使用的电源。例如,当x是一个随机100x100的阵列

Maybe I'm doing something odd, but maybe found a surprising performance loss when using numpy, seems consistent regardless of the power used. For instance when x is a random 100x100 array

x = numpy.power(x,3) 

大约60倍比

x = x*x*x

在加快各种数组大小的一个情节透着甜蜜点周围大小10K和一致的5到10倍的速度为其他大小的数组。

A plot of the speed up for various array sizes reveals a sweet spot with arrays around size 10k and a consistent 5-10x speed up for other sizes.

code下测试你自己的机器上(有点乱):

Code to test below on your own machine (a little messy):

import numpy as np
from matplotlib import pyplot as plt
from time import time

ratios = []
sizes = []
for n in np.logspace(1,3,20).astype(int):
    a = np.random.randn(n,n)

    inline_times = []
    for i in range(100):
        t = time()
        b = a*a*a
        inline_times.append(time()-t)
    inline_time = np.mean(inline_times)

    pow_times = []
    for i in range(100):
        t = time()
        b = np.power(a,3)
        pow_times.append(time()-t)
    pow_time = np.mean(pow_times)

    sizes.append(a.size)
    ratios.append(pow_time/inline_time)

plt.plot(sizes,ratios)
plt.title('Performance of inline vs numpy.power')
plt.ylabel('Nx speed-up using inline')
plt.xlabel('Array size')
plt.xscale('log')
plt.show()

任何人有一个解释?

Anyone have an explanation?

推荐答案

这是众所周知的双倍增,你的处理器可以在一个非常奇特的方式做的,是非常非常快。 POW 是决然慢。

It's well known that multiplication of doubles, which your processor can do in a very fancy way, is very, very fast. pow is decidedly slower.

一些性能的导游那里甚至建议人们考虑这一点,也许甚至在某种程度上,可能是有时有点过分热心。

Some performance guides out there even advise people to plan for this, perhaps even in some way that might be a bit overzealous at times.

numpy的特殊情况下,平方,以确保它不是太,太缓慢,但它发出多维数据马上到你的libc的 POW ,这是不是几乎一样快一对夫妇的乘法。

numpy special-cases squaring to make sure it's not too, too slow, but it sends cubing right off to your libc's pow, which isn't nearly as fast as a couple multiplications.

这篇关于为什么比衬里慢numpy.power 60倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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