I was curious about boolean optimizations, so I made a small program to test these equivalent expressions:
(A && B) || (!A && !B)
!(A ^ B)
Here is the program on Ideone: https://ideone.com/cr3guM
I expected the shorter expression to be slightly faster, but according to my tests, whichever one runs first is always the fastest. Even if I run them back to back, or alternate between the tests, the first one is always fastest, and so are future executions of that same method.
Example:
timeLongForm(); // 879ms
timeShortForm(); // 1316ms
timeLongForm(); // 876ms
swapped order:
timeShortForm(); // 917ms
timeLongForm(); // 1375ms
timeShortForm(); // 915ms
What is causing this?