/*
Copyright (c) 2014, Matthew Anderson a.k.a. DrMrLordX
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package mathtester;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class OmniCode extends Thread
{
volatile long totaltime = 0;
volatile long totaltime2 = 0;
final int standard = 16777216;
final int warmup = 10000;
public long runloops(byte selection)
{
this.setPriority(MIN_PRIORITY);
ExecutorService executor = Executors.newFixedThreadPool(48);
for (short j = 0; j < 48; j++)
{
switch (selection)
{
case 1:
executor.execute(new IntegerLoop(warmup));
break;
case 2:
executor.execute(new IntegerLoopNoDiv(warmup));
break;
case 5:
executor.execute(new CastFloatToInt(warmup));
break;
case 6:
executor.execute(new RoundFloatToInt(warmup));
break;
case 7:
executor.execute(new CastIntToFloat(warmup));
break;
case 11:
executor.execute(new OmniLoop((byte)1, warmup));
break;
case 12:
executor.execute(new OmniLoop((byte)2, warmup));
break;
case 13:
executor.execute(new OmniLoop((byte)3, warmup));
break;
case 14:
executor.execute(new OmniLoop((byte)4, warmup));
break;
case 15:
executor.execute(new OmniLoop((byte)5, warmup));
break;
case 16:
executor.execute(new OmniLoop((byte)6, warmup));
break;
case 17:
executor.execute(new OmniLoop((byte)7, warmup));
break;
case 18:
executor.execute(new OmniLoop((byte)8, warmup));
break;
case 19:
executor.execute(new OmniLoop((byte)9, warmup));
break;
case 110:
executor.execute(new OmniLoop((byte)10, warmup));
break;
case 111:
executor.execute(new OmniLoop((byte)11, warmup));
break;
default:
break;
}
}
//totaltime = System.currentTimeMillis();
executor.shutdown();
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
executor = Executors.newFixedThreadPool(48);
for (short j = 0; j < 48; j++)
{
switch (selection)
{
case 1:
executor.execute(new IntegerLoop(standard));
break;
case 2:
executor.execute(new IntegerLoopNoDiv(standard));
break;
case 5:
executor.execute(new CastFloatToInt(standard));
break;
case 6:
executor.execute(new RoundFloatToInt(standard));
break;
case 7:
executor.execute(new CastIntToFloat(standard));
break;
case 11:
executor.execute(new OmniLoop((byte)1, standard));
break;
case 12:
executor.execute(new OmniLoop((byte)2, standard));
break;
case 13:
executor.execute(new OmniLoop((byte)3, standard));
break;
case 14:
executor.execute(new OmniLoop((byte)4, standard));
break;
case 15:
executor.execute(new OmniLoop((byte)5, standard));
break;
case 16:
executor.execute(new OmniLoop((byte)6, standard));
break;
case 17:
executor.execute(new OmniLoop((byte)7, standard));
break;
case 18:
executor.execute(new OmniLoop((byte)8, standard));
break;
case 19:
executor.execute(new OmniLoop((byte)9, standard));
break;
case 110:
executor.execute(new OmniLoop((byte)10, standard));
break;
case 111:
executor.execute(new OmniLoop((byte)11, standard));
break;
default:
break;
}
}
totaltime = System.currentTimeMillis();
executor.shutdown();
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
totaltime = System.currentTimeMillis() - totaltime;
switch (selection)
{
case 1:
System.out.println("It took " + totaltime + " milliseconds to complete IntegerLoop.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete IntegerLoop.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 2:
System.out.println("It took " + totaltime + " milliseconds to complete IntegerLoopNoDiv.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete IntegerLoopNoDiv.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 5:
System.out.println("It took " + totaltime + " milliseconds to complete CastFloatToInt.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete CastFloatToInt.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 6:
System.out.println("It took " + totaltime + " milliseconds to complete RoundFloatToInt.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete RoundFloatToInt.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 7:
System.out.println("It took " + totaltime + " milliseconds to complete CastIntToFloat.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete CastIntToFloat.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 11:
break;
case 12:
break;
case 13:
System.out.println("It took " + totaltime + " milliseconds to complete integer addition.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete integer addition.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 14:
System.out.println("It took " + totaltime + " milliseconds to complete integer multiplication.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete integer multiplication.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 15:
System.out.println("It took " + totaltime + " milliseconds to complete integer division.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete integer division.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 16:
System.out.println("It took " + totaltime + " milliseconds to complete float addition.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete float addition.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 17:
System.out.println("It took " + totaltime + " milliseconds to complete float multiplication.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete float multiplication.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 18:
System.out.println("It took " + totaltime + " milliseconds to complete float division.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete float division.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 19:
System.out.println("It took " + totaltime + " milliseconds to complete rounding float to "
+ "integer.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete rounding float to "
+ "integer.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 110:
System.out.println("It took " + totaltime + " milliseconds to complete casting float to "
+ "integer.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete casting float to "
+ "integer.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 111:
System.out.println("It took " + totaltime + " milliseconds to complete casting integer to "
+ "float.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime + " milliseconds to complete casting integer to "
+ "float.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
default:
System.out.println("It took " + totaltime + " milliseconds to complete IntegerLoop.");
break;
}
executor = null;
Runtime.getRuntime().gc();
executor = Executors.newFixedThreadPool(48);
for (short j = 0; j < 48; j++)
{
switch (selection)
{
case 1:
executor.execute(new FloatLoop(warmup));
break;
case 2:
executor.execute(new FloatLoopNoDiv(warmup));
break;
case 5:
executor.execute(new CastFloatToIntNoDiv(warmup));
break;
case 6:
executor.execute(new RoundFloatToIntNoDiv(warmup));
break;
case 7:
executor.execute(new CastIntToFloatNoDiv(warmup));
break;
default:
break;
}
}
//totaltime2 = System.currentTimeMillis();
executor.shutdown();
switch (selection)
{
case 1:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 2:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 5:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 6:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 7:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
default:
break;
}
executor = Executors.newFixedThreadPool(48);
for (short j = 0; j < 48; j++)
{
switch (selection)
{
case 1:
executor.execute(new FloatLoop(standard));
break;
case 2:
executor.execute(new FloatLoopNoDiv(standard));
break;
case 5:
executor.execute(new CastFloatToIntNoDiv(standard));
break;
case 6:
executor.execute(new RoundFloatToIntNoDiv(standard));
break;
case 7:
executor.execute(new CastIntToFloatNoDiv(standard));
break;
default:
break;
}
}
totaltime2 = System.currentTimeMillis();
executor.shutdown();
switch (selection)
{
case 1:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 2:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 5:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 6:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
case 7:
while (!executor.isTerminated())
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
//Do nothing
}
}
break;
default:
break;
}
totaltime2 = System.currentTimeMillis() - totaltime2;
switch (selection)
{
case 1:
System.out.println("It took " + totaltime2 + " milliseconds to complete FloatLoop.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime2 + " milliseconds to complete FloatLoop.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 2:
System.out.println("It took " + totaltime2 + " milliseconds to complete FloatLoopNoDiv.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime2 + " milliseconds to complete FloatLoopNoDiv.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 5:
System.out.println("It took " + totaltime2 + " milliseconds to complete CastFloatToIntNoDiv.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime2 + " milliseconds to complete CastFloatToIntNoDiv.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 6:
System.out.println("It took " + totaltime2 + " milliseconds to complete RoundFloatToIntNoDiv.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime2 + " milliseconds to complete RoundFloatToIntNoDiv.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
case 7:
System.out.println("It took " + totaltime2 + " milliseconds to complete CastIntToFloatNoDiv.");
if (Mathtester.willprint)
{
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("results.txt", true))))
{
out.println("It took " + totaltime2 + " milliseconds to complete CastIntToFloatNoDiv.");
}
catch (IOException e)
{
System.out.println("Input/Output exception, can not complete logging operation.");
Mathtester.willprint = false;
}
}
break;
default:
break;
}
executor = null;
Runtime.getRuntime().gc();
return totaltime + totaltime2;
}
}