final int random =(( randomizer / 65536 )& 0x7fffffff )% 10;
I look more carefully at the code, I see another problem.
Your code to calculate randoms overwhelms the cost of the code to do
the swtich.
I rewrote the code. Here is my new result with Jet.
[E:\com\mindprod\example]TestSwitch
Calculate time for IF/time for SWITCH. >1 means IF is slower
Trial | ratio | average
0 1.00 1.00
1 1.01 1.00
2 0.97 0.99
3 0.99 0.99
4 1.01 1.00
5 1.00 1.00
6 1.04 1.00
7 1.01 1.00
8 1.00 1.00
9 0.99 1.00
10 0.99 1.00
In other words IF is just as fast as switch WHY??
package com.mindprod.example;
import static java.lang.System.nanoTime;
import static java.lang.System.out;
import java.util.Random;
/**
* Compares relative speed of nested if vs switch
*
* @author Roedy Green, Canadian Mind Products, based on a program by
Stefan
* Ram.
* @version 1.0, 2007-07-04 Created with IntelliJ IDEA.
*/
public class TestSwitch {
// ------------------------------ FIELDS
------------------------------
// one million iterations per trial
final static long ITERATIONS_PER_TRIAL = 1000000L;
// --------------------------- main() method
---------------------------
public static void main( final java.lang.String[] args ) throws
java.lang.Throwable
{
out.println(
"Calculate time for IF/time for SWITCH. >1 means IF
is slower" );
out.println( " Trial | ratio | average" );
final Random wheel = new Random();
// accumulate total machine cycles for switch
long accumSwitchTotal = 0;
// accumulate total machine cycles for if
long accumIfTotal = 0;
// force code to do something rather than be optimised out of
existence
long forcer = 0;
// do forever
for ( long trial = 0; ; trial++ )
{
// accumulate machine cycles for switch for this trial
long accumSwitchTrial = 0;
// accumulate machine cycles for if for this trial
long accumIfTrial = 0;
for ( long i = 0; i < ITERATIONS_PER_TRIAL; ++i )
{
final int random = wheel.nextInt( 10 );
long start = nanoTime();
final int rif;
{
if ( random == 0 )
{
rif = 20418437;
}
else if ( random == 1 )
{
rif = 94704581;
}
else if ( random == 2 )
{
rif = 45898144;
}
else if ( random == 3 )
{
rif = 49094059;
}
else if ( random == 4 )
{
rif = 77416885;
}
else if ( random == 5 )
{
rif = 91322469;
}
else if ( random == 6 )
{
rif = 40218964;
}
else if ( random == 7 )
{
rif = 93641667;
}
else if ( random == 8 )
{
rif = 29822916;
}
else
{
rif = 625909;
}
}
accumSwitchTrial += nanoTime() - start;
forcer += rif;
start = nanoTime();
final int rswitch;
switch ( random )
{
case 0:
rswitch = 16336774;
break;
case 1:
rswitch = 62881795;
break;
case 2:
rswitch = 27998504;
break;
case 3:
rswitch = 9612956;
break;
case 4:
rswitch = 25592297;
break;
case 5:
rswitch = 77114095;
break;
case 6:
rswitch = 23554325;
break;
case 7:
rswitch = 55380051;
break;
case 8:
rswitch = 47971513;
break;
default:
rswitch = 41100793;
}
accumIfTrial += nanoTime() - start;
forcer += rswitch;
}// end for iterations
double ratio = (double) accumIfTrial / (double)
accumSwitchTrial;
accumIfTotal += accumIfTrial;
accumSwitchTotal += accumSwitchTrial;
double averageRatio =
(double) accumIfTotal / (double) accumSwitchTotal;
out.printf( "%6d %6.02f %6.02f\n", trial, ratio,
averageRatio );
}
}
}