• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with hobby project

DrMrLordX

Lifer
I hesitate to start another thread related to a topic already being discussed in another AT subforum, but I've come to ask for help, so technically it's not the same thing. I think.

Mods, feel free to nuke this thread if you want me to take it back to the CPU forum. Anyway . . .

Not so long ago, I set out to run some simple (if poorly thought-out) mathematical operations in loops spawned in a multitude of threads to see which operations were going to be fastest on my machines. The source is here:

https://www.dropbox.com/s/l3wlxax4mpigzb9/mathtester1032014source.zip?dl=0
(it's got a BSD 2-clause license on it, for what it's worth)

It was (and still is) all written in Java. Several builds later, I'm pretty happy with the current incarnation of the original code (aka Awesomeballs . . . Maximilian comes up with the best project names) even if I harbor the suspicion that I could handle the threads with lower overhead somehow (and even if the code is still of questionable quality). But that is beside the point.

The new "isolated tests" were designed to remove loop overhead from the reported time of execution. They were also designed to make the results less addition-heavy (the original code features much more addition than anything else). I've encountered two obvious problems (there may be myriad other problems that I just haven't noticed yet):

1). The isolated test loops (in OmniLoop) were designed so that I could just run the blankintloop/blankfloatloop methods, record their run times, and then subtract those times from the run times of other methods in OmniLoop as a way to eliminate loop overhead from the results. This approach has proven problematic. For whatever reason, the "blank" loops occasionally take more time to execute than the non-"blank" loops, which makes no sense. For a few tests (the one that casts int to float, I think), it returns a negative result every darn time.

2). If you look at Biostud's results from the 10/3 build, you'll see that he turns in a time of 47 ms for the integer division test. He has an i7-5820k @ 4.4 ghz. My junky little Stars tri-core (unlocked x2-220) @ 3.6 ghz turned in a time of 32 ms. On an earlier build (9/30) he beat me with a time of 27ms, but still, the results should have shown a larger performance delta than that.

That just makes no sense. There's no way a CPU at a higher clockspeed, with higher IPC, and the ability to handle four times as many threads is going to be slower in anything multithreaded. True, I haven't gotten results on that particular test from any other Haswell CPUs (or Stars CPUs), but still . . . there is almost assuredly something wrong with my code. I just can't figure out what. The Haswell should retire unwanted NOPs and handle the "main" thread and its recurring thread.sleep(1) overhead better than my x2-220, any day of the week. Something's fishy there.

If anyone can offer some suggestions on how to deal with these problems (especially problem 1), I'd appreciate it. Thanks!
 
I'd suggest not testing integer division. It's always a tough operation, so most people, and compiler, and presumably libraries, avoid it like the plague. So I wouldn't be surprised if newer processors require more cycles for integer division.

Oh, and was one test done on a 32-bit operating system, while another was done on 64-bit? That might make a difference.

By the way, if you want to see the results of another person's tests on CPUs, this time in C/assembly, look here: http://www.agner.org/optimize/
 
I'd suggest not testing integer division. It's always a tough operation, so most people, and compiler, and presumably libraries, avoid it like the plague. So I wouldn't be surprised if newer processors require more cycles for integer division.

I have heard a good bit about that. Integer division and floating point division have both been pretty slow. In the older code, I actually made versions of the test with the division operations removed (hence the "nodiv" label). Those tests tend to be much faster.

The weird thing is that, after warmup, the isolated loop that attempts to test nothing but integer division is just as fast as integer add and integer mult. Makes me think the VM is ignoring some or all of the division operations. The division operations definitely slowed things to a crawl in the older code, even if there was only one division per loop iteration. In contrast, there were six addition and one multiplication operations in the same loop.

Oh, and was one test done on a 32-bit operating system, while another was done on 64-bit? That might make a difference.

To the best of my knowledge, all of the results have come from 64-bit operating systems, though I could be wrong. I know all the results on my end have been from 64-bit OSes.

By the way, if you want to see the results of another person's tests on CPUs, this time in C/assembly, look here: http://www.agner.org/optimize/

That may come in handy. It's been a long time since I used C or C++. I need to brush up.

edit: truth be told, the code that is causing me trouble is only a small amount of everything in the .zip file. So I'll post the relevant bits:

Here is OmniCode, which is where I put the timing code, start the ExecutorService, and start up all 48 tasks for the chosen test:

Code:
/*
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;
	}
}

Here's OmniLoop. This class contains all the loops for the new isolated tests. The first two (blankintloop and blankfloatloop) were intended to serve as baselines so that I could run one of them, then run one of the other loops, and subtract the run time of the blank loop from the loop that actually has some loop contents (other than an internal increment). The code as-posted does not have that feature (since I have not been able to make it work yet):
Code:
/*
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;

public class OmniLoop extends Thread
{
	byte selection = 0;
	int i = 0;
	int j = 0;
	int int1 = 0;
	int int2 = 0;
	int int3 = 0;
	float float1 = 0;
	float float2 = 0;
	float float3 = 0;
	int iterations = 0;
	
	public OmniLoop(byte input, int cycles)
	{
		selection = input;
		iterations = cycles;
	}
	
	public void run()
	{
		this.setPriority(9);
		
		switch (selection)
		{
			case 1:
				blankintloop();
				break;
			case 2:
				blankfloatloop();
			case 3:
				intaddloop();
				break;
			case 4:
				intmultloop();
				break;
			case 5:
				intdivloop();
				break;
			case 6:
				floataddloop();
				break;
			case 7:
				floatmultloop();
				break;
			case 8:	
				floatdivloop();
				break;
			case 9:
				roundfloattointloop();
				break;
			case 10:
				castfloattointloop();
				break;
			case 11:
				castinttofloatloop();
				break;			
		}
	}
	
	public void blankintloop()
	{
		do
		{
			i = 0;
			int1 = 1;
			int2 = 0;
			
			do
			{		
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void blankfloatloop()
	{
		do
		{
			int1 = 1;
			int2 = 0;
			float1 = 0;
			
			do
			{		
				float1++;
			}
			while (float1 < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void intaddloop()
	{
		do
		{
			i = 0;
			int1 = 1;
			int2 = 0;
			
			do
			{		
				int2 += int1;
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void intmultloop()
	{
		do
		{
			i = 0;
			int1 = 2;
			int2 = 1;
			
			do
			{		
				int2 *= int1;
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void intdivloop()
	{
		do
		{
			i = 0;
			int1 = 3;
			int2 = 0;
			
			do
			{		
				int2 = i / int1;
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void floataddloop()
	{
		do
		{
			i = 0;
			float1 = 1;
			float2 = 0;
			
			do
			{		
				float2 += float1;
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void floatmultloop()
	{
		do
		{
			i = 0;
			float1 = 2;
			float2 = 1;
			
			do
			{		
				float2 *= float1;
				i++;
			}
			while (i < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void floatdivloop()
	{
		do
		{
			float3 = 0;
			float1 = 3;
			float2 = 0;
			
			do
			{		
				float2 = float3 / float1;
				float3++;
			}
			while (float3 < 10);
			j++;
		}
		while (j < iterations);
	}
	
	public void roundfloattointloop()
	{
		do
		{
			float1 = 1;
			int1 = 0;
			int2 = 0;
			
			do
			{		
				int2 = Math.round(float1);
				float1++;
			}
			while (float1 < 11);
			j++;
		}
		while (j < iterations);
	}
	
	public void castfloattointloop()
	{
		do
		{
			float1 = 1;
			int1 = 0;
			int2 = 0;
			
			do
			{		
				int2 = (int)(float1);
				float1++;
			}
			while (float1 < 11);
			j++;
		}
		while (j < iterations);
	}
	
	public void castinttofloatloop()
	{
		do
		{
			i = 1;
			int1 = 1;
			float2 = 0;
			
			do
			{		
				float2 = (float)(i);
				i++;
			}
			while (i < 11);
			j++;
		}
		while (j < iterations);
	}
}

I can rewrite it to include the subtraction if that would help.
 
Last edited:
Back
Top