package ajmas74.experiments; /** * @author Andre-John Mas * *

Negativity Change Speed Test

*

* In mathematical equations you sometime need to be able * to invert the sign of a component in equations ever other * iteration, for example: *

 *   + 3 - 2 + 4 - 10 ...
 * 
* This class was originally created to test whether a condition * was faster than Math.pow() for changing the sign. This is because * in maths, given 'i' as the iteration value, you define -1^i (note: * in Java we don't use the caret, '^', for power since this actually * a 'bitwise xor' operator). *

*

In the end it was proved that the condition (test 2) was actually, * faster than the Math.pow() approach (test 4), even if we compensate * by placing the condition in another method (test 1). A further test * should that we can replace the condition with a multiplication and * gain even more speed (test 3). *

*

* It should be noted that for very small iterations the difference in * time is small, but the larger the iteration, the bigger the gap. We * can draw a simple graph of the speed differences: *

 *    (test 3)(test 1)(test 2)                        (test 4)
 *       |       |       |                               |
 *     ---------------------------------------------------
 * 
 * 

*

I provide this class as a reference for anyone wanting to * with this sort of operation to improve performance. *

*/ public class NegChangeSpeedTest { private static void test1 ( int maxIteration ) { long startTime = System.currentTimeMillis(); int n = 0; for ( int i=0; i