I and my friend wanted to try out which of the following way would help us decide the best way to execute a certain set of a statement if any three boolean variable is true. If it was this one or:
if (a || b || c)
//statements
or this one
if (a) //statements;
else if (b) //statement;
else if (c) //statement;
so we ran a couple of tests to find out which is faster and found out that second one is faster than the second one is faster than the first one
Test Program:
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
public class Hola {
public static void main(String[] args) {
boolean a=true,b=false,c=true;
long time;
long ifOrSum =0;
long ifElseSum =0;
for(int i =1;i<=1000000000;i++) {
time = System.nanoTime();
if (a || b || c) ;
ifOrSum+=System.nanoTime() -time;
}
System.out.print("hey");
for(int i =1;i<=1000000000;i++) {
time = System.nanoTime();
if (a);
else if(b);
else if(c);
ifOrSum+=System.nanoTime() -time;
}
double d =(ifOrSum-ifElseSum)/1000000000;
System.out.print(d);
}
}
The program executes for around 4 minutes and the difference is around 70-80 seconds we would like to know why this 76 seconds of delay is being caused.
Aucun commentaire:
Enregistrer un commentaire