Java - Math.pow() 和 Math.sqrt() 的更快替代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15101945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:29:04  来源:igfitidea点击:

Java - Faster alternative to Math.pow() and Math.sqrt()

javafunctionmathfloating-accuracypow

提问by Matt9Atkins

My program uses Math.pow()to compute a relatively large double number to the power of 2. Later on I need to find the square root of a very large double number. The problem is, I have to do this over a 100,000 times and it is taking really long. Is there any alternative that can speed this process up? Thanks

我的程序用来Math.pow()计算一个相对较大的 double 数的 2 次方。稍后我需要找到一个非常大的 double 数的平方根。问题是,我必须这样做超过 100,000 次,而且需要很长时间。有没有其他方法可以加快这个过程?谢谢

Edit: By large numbers I mean between 1000 to 10000 (So probably not that large in computing terms). And in terms of it taking a long time, it takes about 30 secs to do the function 500 times

编辑:大数字我的意思是在 1000 到 10000 之间(所以在计算方面可能没有那么大)。而在耗时较长的情况下,执行500次该功能大约需要30秒

回答by Oleksi

You are unlikely to find a better(faster) implementation than the Java Math one. You might have more luck trying to change the way you do the calculations in your algorithm. For example, is there any way you can avoid finding the square root of a huge number?

您不太可能找到比 Java Math 更好(更快)的实现。尝试更改算法中的计算方式可能会更幸运。例如,有什么方法可以避免找到一个巨大数字的平方根?

If this doesn't work, you can try implementing it in a more appropriate language that's meant for fast mathematical computations (something like Matlab).

如果这不起作用,您可以尝试使用更合适的语言来实现它,该语言用于快速数学计算(例如 Matlab)。

Otherwise, you can try to optimize this in other areas. Perhaps you can try to cache past results if they are useful later.

否则,您可以尝试在其他方面对此进行优化。如果以后有用,也许您可​​以尝试缓存过去的结果。

回答by Carl Smotricz

"The power of 2" is squaring. You'd be better off doing that by multiplying the number by itself.

“2的幂”是平方。你最好通过将数字乘以它本身来做到这一点。

The library version of sqrtis probably faster than anything you could dig up elsewhere. If you call a C routine, you'll just add overhead from the cross-language call. But do you need accurate square roots, or would a table lookup of approximations do? Do the values repeat a lot, i.e. do you often need to calculate the roots of the same numbers? If so, caching the square roots in a HashMapmight be faster than computing them.

的库版本sqrt可能比您在其他地方挖掘的任何内容都快。如果调用 C 例程,则只会增加跨语言调用的开销。但是您是否需要精确的平方根,或者近似值的表查找可以吗?这些值是否重复很多,即您是否经常需要计算相同数字的根?如果是这样,在 a 中缓存平方根HashMap可能比计算它们更快。

回答by Lewis Richard Phillip Cowles

the only thing I can think of is storing the results for speed, the square root will not change, and ~9000 stored numbers is not that many. You would probably do well to frame your data, so that you could ensure that you can optimally search for the appropriate result.

我唯一能想到的就是存储速度的结果,平方根不会改变,并且 ~9000 个存储的数字并不多。您可能会很好地构建您的数据,以便您可以确保您可以以最佳方式搜索适当的结果。

回答by Mark Beleski

Well your problem with the power of 2 can simply be done by multiplying the number by itself. For example, lets say variable a is the number you want to raise to 2. It's the same as: int a=5; int b=a*a;

那么您的 2 次幂问题可以简单地通过将数字乘以本身来完成。例如,假设变量 a 是您想要提高到 2 的数字。它与以下内容相同: int a=5; int b=a*a;

回答by fso

You can use x*x instead of pow(x, 2).

您可以使用 x*x 代替 pow(x, 2)。

For square root, you should first take a look on sqrt implementation (approximation method).

对于平方根,你应该先看看sqrt的实现(近似法)。

Maybe you can find a better one, for example the Newton's method(on equation sqrt(N)-x=0).

也许您可以找到更好的方法,例如牛顿法(方程 sqrt(N)-x=0)。

It also depends on the needed accuracy, you can trade accuracy vs time.

它还取决于所需的准确性,您可以在准确性与时间之间进行交易。

You can also store results to avoid multiple computations on the same entries.

您还可以存储结果以避免对同一条目进行多次计算。