public class BenchSimpRad
{
public static final int TEST_VALUE = 1274912425; // This will simpilify to 205 root 30337
static long[] simplify2( long square )
{
double outside = 1;
double inside = square;
long[] simplified = { 1, square };
// Check if it's already a perfect square
outside = Math.sqrt(square);
if (outside == Math.floor(outside))
{
simplified[0] = (long)outside;
simplified[1] = 1;
return simplified;
}
// Find all the squares that could be factors and see if they are
long i, sqr;
for (i = 2, sqr = 4; sqr <= (square / 2); i++, sqr = (i * i))
{
// Is this square a factor?
double in = (double)square / sqr;
if (in == Math.floor(in))
{
simplified[0] = i;
simplified[1] = (long)in;
}
}
// Otherwise, since it hasn't simplified, return the original radical
return simplified;
}
static long[] simplify4( long square )
{
long outside = 1;
long inside = square;
long[] simplified = { 1, square };
// Check if it's already a perfect square
outside = (long)Math.sqrt(square);
if ((outside * outside) == square)
{
simplified[0] = outside;
simplified[1] = 1;
return simplified;
}
// Find all the squares that could be factors and see if they are
for (long factor = 2, sqr = 4; sqr <= (square / 2); factor++, sqr = (factor * factor))
{
// Is this square a factor?
if ((square % sqr) == 0)
{
simplified[0] = factor;
simplified[1] = square / sqr;
}
}
// Otherwise, since it hasn't simplified, return the original radical
return simplified;
}
public static void benchmark2( int iterations )
{
long startTime = System.nanoTime();
for (int i = 0; i < iterations; i++)
{
simplify2(TEST_VALUE);
}
long endTime = System.nanoTime();
long avgTime = (endTime - startTime) / iterations;
System.out.println("SimpRad2 took " + avgTime + "ns per iteration on average over "
+ iterations + " iterations.");
}
public static void benchmark4( int iterations )
{
long startTime = System.nanoTime();
for (int i = 0; i < iterations; i++)
{
simplify4(TEST_VALUE);
}
long endTime = System.nanoTime();
long avgTime = (endTime - startTime) / iterations;
System.out.println("SimpRad4 took " + avgTime + "ns per iteration on average over "
+ iterations + " iterations.");
}
public static void benchmark2JIT( int iterations )
{
for (int i = 0; i < 500; i++)
{
simplify2(TEST_VALUE);
}
long startTime = System.nanoTime();
for (int i = 0; i < iterations; i++)
{
simplify2(TEST_VALUE);
}
long endTime = System.nanoTime();
long avgTime = (endTime - startTime) / iterations;
System.out.println("SimpRad2 took " + avgTime + "ns per iteration on average over "
+ iterations + " iterations.");
}
public static void benchmark4JIT( int iterations )
{
for (int i = 0; i < 500; i++)
{
simplify4(TEST_VALUE);
}
long startTime = System.nanoTime();
for (int i = 0; i < iterations; i++)
{
simplify4(TEST_VALUE);
}
long endTime = System.nanoTime();
long avgTime = (endTime - startTime) / iterations;
System.out.println("SimpRad4 took " + avgTime + "ns per iteration on average over "
+ iterations + " iterations.");
}
public static void main( String[] args )
{
// BENCHMARK SIMPRAD2
// Run 500 iterations to benchmark without JIT
benchmark2(50);
benchmark4(50);
// Run another 10000 iterations to benchmark with JIT
benchmark2JIT(10000);
benchmark4JIT(10000);
}
}
Comments