gilou31120
Messages postés2Date d'inscriptiondimanche 22 janvier 2006StatutMembreDernière intervention25 septembre 2006 25 sept. 2006 à 20:19
Petite optimization en vitesse de la méthode toInteger:
Avant :
public int toInteger() {
int res = 0;
for (int index = 0; index != this.length(); ++index)
if (_value[index])
res += Math.pow(2, this.length() - index -1);
return res;
}
Après:
public int toInteger() {
/* initialize the result with the most significant bit */
int res = (_value[0] ? 1 : 0);
for (int index = 1; index < this.length(); index++)
{
/* shift left then add one bit */
res = 2 * res;
res += (_value[index] ? 1 : 0);
}
return res;
}
25 sept. 2006 à 20:19
Avant :
public int toInteger() {
int res = 0;
for (int index = 0; index != this.length(); ++index)
if (_value[index])
res += Math.pow(2, this.length() - index -1);
return res;
}
Après:
public int toInteger() {
/* initialize the result with the most significant bit */
int res = (_value[0] ? 1 : 0);
for (int index = 1; index < this.length(); index++)
{
/* shift left then add one bit */
res = 2 * res;
res += (_value[index] ? 1 : 0);
}
return res;
}