Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- class IntegerOperationTest
- {
- public static final int ITERATION_COUNT = 500000000;
- public static void main(String[] args)
- {
- runPureAddIntTest();
- runPureSubtractIntTest();
- runPureFloatAddTest();
- runPureSubstractFloatTest();
- runPureIntPowerTest();
- runPurePowerFloatTest();
- runPureDivisionFloatTest();
- runAddIntTest();
- runSubstractIntTest();
- runFloatAddTest();
- runSubstractFloatTest();
- runIntPowerTest();
- runFloatPowerTest();
- runIntModuloTest();
- runFloatModuloTest();
- }
- public static void runPureAddIntTest()
- {
- int i = 0;
- int a = 0;
- int b = 0;
- int c = 0;
- int d = 0;
- int f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- a++;
- b++;
- c++;
- d++;
- f++;
- }
- System.out.println("Pure addition int test complete in: " + (new Date().getTime() - time));
- }
- public static void runPureSubtractIntTest()
- {
- int i = 0;
- int a = Integer.MAX_VALUE;
- int b = Integer.MAX_VALUE;
- int c = Integer.MAX_VALUE;
- int d = Integer.MAX_VALUE;
- int f = Integer.MAX_VALUE;
- long time = new Date().getTime();
- for(i = ITERATION_COUNT; i > 0; i--)
- {
- a--;
- b--;
- c--;
- d--;
- f--;
- }
- System.out.println("Pure subtract int test complete in: " + (new Date().getTime() - time));
- }
- public static void runPureFloatAddTest()
- {
- int i = 0;
- float a = 0;
- float b = 0;
- float c = 0;
- float d = 0;
- float f = 0;
- float increment1 = 0.1f;
- float increment2 = 0.2f;
- float increment3 = 0.3f;
- float increment4 = 0.4f;
- float increment5 = 0.5f;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- a += increment1;
- b += increment2;
- c += increment3;
- d += increment4;
- f += increment5;
- }
- System.out.println("Pure addition float test complete in: " + (new Date().getTime() - time));
- }
- public static void runPureSubstractFloatTest()
- {
- int i = 0;
- float a = 0;
- float b = 0;
- float c = 0;
- float d = 0;
- float f = 0;
- float increment1 = 0.1f;
- float increment2 = 0.2f;
- float increment3 = 0.3f;
- float increment4 = 0.4f;
- float increment5 = 0.5f;
- long time = new Date().getTime();
- for(i = ITERATION_COUNT; i > 0; i--)
- {
- a -= increment1;
- b -= increment2;
- c -= increment3;
- d -= increment4;
- f -= increment5;
- }
- System.out.println("Pure subtract float test complete in: " + (new Date().getTime() - time));
- }
- public static void runPureIntPowerTest()
- {
- int i = 0;
- int a = 0;
- int b = 0;
- int c = 0;
- int d = 0;
- int f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- a *= 2;
- b *= 2;
- c *= 2;
- d *= 2;
- f *= 2;
- }
- System.out.println("Pure power int test complete in: " + (new Date().getTime() - time));
- }
- public static void runPurePowerFloatTest()
- {
- int i = 0;
- float a = 0;
- float b = 0;
- float c = 0;
- float d = 0;
- float f = 0;
- float increment1 = 0.1f;
- float increment2 = 0.2f;
- float increment3 = 0.3f;
- float increment4 = 0.4f;
- float increment5 = 0.5f;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- a *= increment1;
- b *= increment2;
- c *= increment3;
- d *= increment4;
- f *= increment5;
- }
- System.out.println("Pure power float test complete in: " + (new Date().getTime() - time));
- }
- public static void runPureDivisionFloatTest()
- {
- int i = 0;
- float a = Float.MAX_VALUE;
- float b = Float.MAX_VALUE;
- float c = Float.MAX_VALUE;
- float d = Float.MAX_VALUE;
- float f = Float.MAX_VALUE;
- float div1 = 1.0001f;
- float div2 = 1.0002f;
- float div3 = 1.0003f;
- float div4 = 1.0004f;
- float div5 = 1.0005f;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- a /= div1;
- b /= div2;
- c /= div3;
- d /= div4;
- f /= div5;
- }
- System.out.println("Pure division float test complete in: " + (new Date().getTime() - time));
- }
- public static void runAddIntTest()
- {
- int i = 0;
- int c = 0;
- int m = 0;
- int f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(c % 2 == 0)
- {
- c++;
- f += c;
- }
- else
- {
- c += 2;
- f += c;
- }
- if(m % 2 == 0)
- {
- m++;
- f += m;
- }
- else
- {
- m += 2;
- f += m;
- }
- }
- System.out.println("Addition int test complete in: " + (new Date().getTime() - time));
- }
- public static void runSubstractIntTest()
- {
- int i = 0;
- int c1 = 1;
- int c2 = 2;
- int d = Integer.MAX_VALUE;
- int m = Integer.MAX_VALUE;
- int f = Integer.MAX_VALUE;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(d % 2 == 0)
- {
- d--;
- f -= c1;
- }
- else
- {
- d -= c2;
- f -= c2;
- }
- if(m % 2 == 0)
- {
- m--;
- f -= c1;
- }
- else
- {
- m -= c2;
- f -= c2;
- }
- }
- System.out.println("Subtract int test complete in: " + (new Date().getTime() - time));
- }
- public static void runFloatAddTest()
- {
- int i = 0;
- float c = 0;
- float m = 0;
- float f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(i % 2 == 0)
- {
- c++;
- f += c;
- }
- else
- {
- c += 2;
- f += c;
- }
- if(i % 2 == 0)
- {
- m++;
- f += m;
- }
- else
- {
- m += 2;
- f += m;
- }
- }
- System.out.println("Addition float test complete in: " + (new Date().getTime() - time));
- }
- public static void runSubstractFloatTest()
- {
- int i = 0;
- float c1 = 1.1f;
- float c2 = 2.2f;
- float d = Float.MAX_VALUE;
- float m = Float.MAX_VALUE;
- float f = Float.MAX_VALUE;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(i % 2 == 0)
- {
- d--;
- f -= c1;
- }
- else
- {
- d -= c2;
- f -= c2;
- }
- if(i % 2 == 0)
- {
- d--;
- f -= c1;
- }
- else
- {
- d -= c2;
- f -= c2;
- }
- }
- System.out.println("Subtract float test complete in: " + (new Date().getTime() - time));
- }
- public static void runIntPowerTest()
- {
- int i = 0;
- int c = 0;
- int c1 = 2;
- int c2 = 3;
- int m = 0;
- int f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(c % 2 == 0)
- {
- c *= c1;
- f *= c;
- }
- else
- {
- c *= c2;
- f *= c;
- }
- if(m % 2 == 0)
- {
- m *= c1;
- f += m;
- }
- else
- {
- m += c2;
- f += m;
- }
- }
- System.out.println("Power int test complete in: " + (new Date().getTime() - time));
- }
- public static void runFloatPowerTest()
- {
- int i = 0;
- float c = 0;
- float c1 = 1.5f;
- float c2 = 1.9f;
- float m = 0;
- float f = 0;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(i % 2 == 0)
- {
- c *= c1;
- f *= c;
- }
- else
- {
- c *= c2;
- f *= c;
- }
- if(i % 2 == 0)
- {
- m *= c1;
- f += m;
- }
- else
- {
- m += c2;
- f += m;
- }
- }
- System.out.println("Power float test complete in: " + (new Date().getTime() - time));
- }
- public static void runIntModuloTest()
- {
- int i = 0;
- int c = 2;
- int c1 = 2;
- int c2 = 3;
- int m = 4;
- int f = 8;
- boolean hasRemainder;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(i % 2 == 0)
- {
- hasRemainder = c % c1 == 0;
- hasRemainder = f % c == 0;
- }
- else
- {
- hasRemainder = c % c2 == 0;
- hasRemainder = f % c == 0;
- }
- if(i % 2 == 0)
- {
- hasRemainder = m % c1 == 0;
- hasRemainder = f % m == 0;
- }
- else
- {
- hasRemainder = m % c2 == 0;
- hasRemainder = f % m == 0;
- }
- }
- System.out.println("Modulo int test complete in: " + (new Date().getTime() - time));
- }
- public static void runFloatModuloTest()
- {
- int i = 0;
- float c = 2.0f;
- float c1 = 1.5f;
- float c2 = 1.9f;
- float m = 4.0f;
- float f = 8.0f;
- boolean hasRemainder;
- long time = new Date().getTime();
- for(i = 0; i <= ITERATION_COUNT; i++)
- {
- if(i % 2 == 0)
- {
- hasRemainder = c % c1 == 0.0f;
- hasRemainder = f % c == 0.0f;
- }
- else
- {
- hasRemainder = c % c2 == 0.0f;
- hasRemainder = f % c == 0.0f;
- }
- if(i % 2 == 0)
- {
- hasRemainder = m % c1 == 0.0f;
- hasRemainder = f % m == 0.0f;
- }
- else
- {
- hasRemainder = m % c2 == 0.0f;
- hasRemainder = f % m == 0.0f;
- }
- }
- System.out.println("Modulo float test complete in: " + (new Date().getTime() - time));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement