最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

performance - Options to speed up Math.pow() in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I have some code which requires many Math.pow() function calls per second. In testing it seems to be a very large bottleneck to the performance of the code.

The results don't need to be precise - an accuracy of anywhere upwards of 85% should done fine - but my question would be is there any way I can somehow speed these calculations up? Maybe at the cost of some precision?

Edit: these calculations are very unlikely to repeat so a cache wouldn't work.

I have some code which requires many Math.pow() function calls per second. In testing it seems to be a very large bottleneck to the performance of the code.

The results don't need to be precise - an accuracy of anywhere upwards of 85% should done fine - but my question would be is there any way I can somehow speed these calculations up? Maybe at the cost of some precision?

Edit: these calculations are very unlikely to repeat so a cache wouldn't work.

Share Improve this question asked May 6, 2015 at 18:21 user11406user11406 1,2582 gold badges14 silver badges26 bronze badges 6
  • Is there anything in mon between them (e.g. is the power always an integer or always the same)? – redbmk Commented May 6, 2015 at 18:29
  • @redbmk Yes, the power is always going to be an int between 2-5 – user11406 Commented May 6, 2015 at 18:31
  • 1 can you please share us the relevant piece of code that does the Math.pow? Perhaps we can help you there, it's not very easy, unluckily, to directly guess what a faster solution could be. If we can't help you there, you might try codereview too, maybe some review masters may drastically reduce your code or find a more efficient way of doing maths, who knows! – briosheje Commented May 6, 2015 at 18:32
  • 1 @user11406, there is a similar question for C/C++: stackoverflow./questions/2347138/…. Can it be helpful in JS? – DWand Commented May 6, 2015 at 18:33
  • 1 I'm ing more from a C/C++ background as well as other performance-critical areas like GLSL, but we generally just avoid pow if we know the exponent in advance and it's small. Like instead of pow(x, 2), just x*x. It's also rather idiomatic in this native territory to do things like: float x2 = x*x; float x4 = x2*x2;. I'd suggest this as a KISS strategy to using a function like pow which is really best-suited when the exponent is not known (varies at runtime) and/or large. – user4842163 Commented May 6, 2015 at 19:46
 |  Show 1 more ment

2 Answers 2

Reset to default 5

at the cost of some precision

How much loss of precision? If you only need correct answers by a factor of 2, you could use bitwise manipulation.

function pow2(n) {
  return 2 << (n-1);
}

console.log(pow2(n) === Math.pow(2, n));

The Number constructor (including number literals) use only floating point numbers. This function converts the floats to 32-bit integers as described here.

Otherwise, I doubt you'll be able to beat the optimized native implementation of Math.pow.

jsPerf is a great tool for trying multiple techniques to find the fastest one.

This could vary quite a bit by browser or operating system, but so far it turns out that Math.pow is much faster in my environment (Chrome 42, 64-bit Linux) until you open up dev tools. With dev tools open, it's slightly faster to multiply the number as many times as you need depending on the power, as in the following example:

function pow(num, pow) {
    var result = num;
    while (--pow) {
       result *= num;
    }
    return result;
}

I'm running out of different ideas, but you can see what I have so far here:

http://jsperf./math-pow-alternatives

There is also a lot of overhead just to calling a function (hundreds of thousands of times). In this case it seems like Math.pow is the way to go, but there might be other ways to improve performance (or at least perceptible performance). If the code is blocking and the browser is experiencing some lag, you might try using web workers, or limiting the number of calculations per frame. You could also try to reduce the amount of function calls, or make sure you're interacting very minimally with the DOM (especially during the calculations, but preferably not at all).

Without a more concise code sample it will be difficult to fine-tune your code's performance.

发布评论

评论列表(0)

  1. 暂无评论