java 整数幂的乘法 vs ^ 运算符 vs Math.pow()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13911180/
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
Multiplication vs ^ operator vs Math.pow() for integer powers
提问by svz
I got curious about the way power calculation is done in Java and the performance of available methods. So I wrote a simple test to check on Math.pow()
, *
and ^
operations.
我对 Java 中进行功率计算的方式以及可用方法的性能感到好奇。所以我写了一个简单的测试来检查Math.pow()
,*
和^
操作。
public static void main(String[] args) {
int SIZE = 100000000;
int[] arr1 = new int[SIZE];
long st1, end1, st2, end2, st3, end3;
st1 = System.currentTimeMillis();
for (int i = 0; i < SIZE; i++) {
arr1[i] = (int) Math.pow(i, 4);
}
end1 = System.currentTimeMillis();
System.out.println("pow: " + (end1 - st1));
arr1 = new int[SIZE];
st2 = System.currentTimeMillis();
for (int i = 0; i < SIZE; i++) {
arr1[i] = i * i * i * i;
}
end2 = System.currentTimeMillis();
System.out.println("mul: " + (end2 - st2));
arr1 = new int[SIZE];
st3 = System.currentTimeMillis();
for (int i = 0; i < SIZE; i++) {
arr1[i] = i^4;
}
end3 = System.currentTimeMillis();
System.out.println(" ^: " + (end3 - st3));
//to prevent optimizations form skipping the calculations
for (int i = 0; i < SIZE; i++) {
if (arr1[i] == 1){
System.out.println(1);
}
}
System.out.println("done");
}
and if the first two results were quite expected:
如果前两个结果在意料之中:
pow: 19253 19128 19205 19145 19185 19130 19162 19177 19191 19157 | 19173
mul: 91 86 91 85 98 90 90 105 87 95 | 92
^: 80 85 80 70 60 65 75 60 70 60 | 71
the third one is a bit confusing. How come ^
is always a bit faster than simple multiplication and which one should be used?
第三个有点混乱。为什么^
总是比简单的乘法快一点,应该使用哪一个?
All the tests were run with JRE 1.7 in similar conditions.
所有测试均在类似条件下使用 JRE 1.7 运行。
回答by Alnitak
The ^
operator is not performing exponentiation - it's a bitwise "exclusive OR" (aka "xor").
该^
经营者不执行幂-这是一个按位“异或”(又名“XOR”)。
Using integer math for 100000000 raised to the fourth power will give incorrect results - a 32-bit integer cannot store numbers that large.
对 100000000 的四次方使用整数数学运算会得到不正确的结果 - 32 位整数无法存储那么大的数字。
Math.pow()
will use floating point arithmetic. The answers may not be 100% accurate due to precision issues, but should be capable of representing the required range of results.
Math.pow()
将使用浮点运算。由于精度问题,答案可能不是 100% 准确,但应该能够代表所需的结果范围。
To get 100% accurate values for numbers that large, you should use the BigInteger
class. However it will not be particularly fast. This is a trade off you have to make when considering accuracy vs performance.
要为这么大的数字获得 100% 准确的值,您应该使用BigInteger
该类。但是它不会特别快。这是您在考虑准确性与性能时必须做出的权衡。
回答by Andreas Dolk
the ^
operator in Java is bitwise exclusive ORand definitaly not similiar to the power function.
在^
Java中操作是按位异或和definitaly不类同的幂函数。
Reference
参考