GLSL:pow vs整数指数乘法 [英] GLSL: pow vs multiplication for integer exponent

查看:489
本文介绍了GLSL:pow vs整数指数乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GLSL中哪个更快?

Which is faster in GLSL:

pow(x, 3.0f);

x*x*x;

?

幂运算性能是否取决于硬件供应商或指数值?

Does exponentiation performance depend on hardware vendor or exponent value?

推荐答案

虽然这肯定是与硬件/供应商/编译器相关的,但像pow()这样的高级数学函数往往比基本操作要昂贵得多.

While this can definitely be hardware/vendor/compiler dependent, advanced mathematical functions like pow() tend to be considerably more expensive than basic operations.

最好的方法当然是同时尝试和基准测试.但是,如果可以简单地替换高级数学函数,那么我认为使用它不会出错.

The best approach is of course to try both, and benchmark. But if there is a simple replacement for an advanced mathematical functions, I don't think you can go very wrong by using it.

如果编写pow(x, 3.0),则可能最好的希望是编译器将识别特殊情况并进行扩展.但是,如果替代品又短又容易阅读,那为什么要冒险呢? C/C ++编译器并不总是通过简单的乘法来替换pow(x, 2.0),因此我不必指望所有GLSL编译器都可以做到这一点.

If you write pow(x, 3.0), the best you can probably hope for is that the compiler will recognize the special case, and expand it. But why take the risk, if the replacement is just as short and easy to read? C/C++ compilers don't always replace pow(x, 2.0) by a simple multiplication, so I wouldn't necessarily count on all GLSL compilers to do that.

这篇关于GLSL:pow vs整数指数乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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