Hice algunas pruebas en el método de pow (exponente). Desafortunadamente, mis habilidades matemáticas no son lo suficientemente fuertes como para manejar el siguiente problema.java.math.BigInteger pow (exponente) pregunta
Estoy usando este código:
BigInteger.valueOf(2).pow(var);
Resultados:
- var | hora en ms
- 2000000 |
- 2500000 |
- 3000000 | 22379
- 3500000 | 32147
- 4000000 |
- 4500000 |
- 5000000 | 49922
Ver? 2,500,000 de exponente se calcula casi tan rápido como 2,000,000. 4,500,000 se calcula mucho más rápido que 4,000,000.
¿Por qué es eso?
Para darle un poco de ayuda, aquí está la implementación original de BigInteger.pow (exponente):
public BigInteger pow(int exponent) {
if (exponent < 0)
throw new ArithmeticException("Negative exponent");
if (signum==0)
return (exponent==0 ? ONE : this);
// Perform exponentiation using repeated squaring trick
int newSign = (signum<0 && (exponent&1)==1 ? -1 : 1);
int[] baseToPow2 = this.mag;
int[] result = {1};
while (exponent != 0) {
if ((exponent & 1)==1) {
result = multiplyToLen(result, result.length,
baseToPow2, baseToPow2.length, null);
result = trustedStripLeadingZeroInts(result);
}
if ((exponent >>>= 1) != 0) {
baseToPow2 = squareToLen(baseToPow2, baseToPow2.length, null);
baseToPow2 = trustedStripLeadingZeroInts(baseToPow2);
}
}
return new BigInteger(result, newSign);
}
hiciste un millón de carreras de cada una de esas llamadas y el promedio de los resultados para obtener el mesa que proporcionaste? – vicatcu
¿Cuántas veces promedia el tiempo? –
@vicatcu: Creo que es seguro asumir que no esperó 3 años para obtener los resultados. –