Consider the case in which >, == and >= operator are used for comparison.
long starttime = System.currentTimeMillis();
for (int i = 0; i < 8888888; i++) {
if (i == 10 || i < 10) {
}
}
System.out.println(“Total time taken case1″+(System.currentTimeMillis()-starttime));
starttime = System.currentTimeMillis();
for (int i = 0; i < 8888888; i++) {
if (i <= 10) {
}
}
System.out.println(“Total time taken case2″+(System.currentTimeMillis()-starttime));
Output is :
Total time taken case1 31ms
Total time taken case2 16ms
I guess not many people fall for 1st case but still i have seen code implemented using case 1.
Just a cautionary Tip.
Cautionary tip? 15ms for 8 million times running it? This is bullshit. The two don’t make a difference at all. You’d have to run it a billion times for anyone to notice anything.
Yep and the benchmark is worth nothing, it will be severaly impacted by Dead Code elimination on any decent compilers. Java cannot be benchmarked like C, please start here:
http://wikis.sun.com/display/HotSpotInternals/MicroBenchmarks
I’m so glad you don’t work on my team. All that time wasted on something so trivial.
ct:
Why so upset? The tip is bad and unfunded… but does it really impact your life that much?
xman:
I’m glad I’m not on *your* team. You clearly have fairly poor interpersonal skills and no doubt make a poor teammate yourself.
It is important not to ‘trick’ the VM or compiler. Compilers and VMs are MUCH smarter than any programmer. If you find a trick like this, tell the compiler/VM writer so they can fix the performance problem for everyone. A good compiler/VM would just optimize that whole if statement out.
*unfounded
Looks like everyone is upset about the tip i made. I can understand that it is trivial and may not make sense.
The reason i wrote it because i was doing the code review for an application for performance improvement and someone had done similar mistake, which i rectified.
I know you all are geeks and experts but not everyone out there is at the same level
It’s hard to make a judgment about the validity of the benchmark. First we need to see *all* of the code that you used. We’d have to understand which build of Java along with the hardware/OS platform you ran on. What the configuration settings were for the run. I’d also want to see richer set of statistics. Any thing less and I’m afraid that it is not possible to make any conclusion.
Oook. 2 minutes wasted. Multiply that by the many who will fail to understand the triviality lying behind the title “java performance tip”, and you’ll realize you managed to waste a few good hours of man-time.
How about “Trivial code nanorefactoring I discovered” next time, it should protect the innocent.
I just tried to inverse the 2 tests : the first test is still faster than the second
Moreover if you do the test twice you obtain the same result.
I added two “8” at the number of occurence
public static void main(String[] args) {
long starttime ;
starttime = System.currentTimeMillis();
for (long i = 0; i < 888888888l; i++) {
if (i == 10 || i < 10) {
}
}
System.out.println(“Total time taken case1 “+(System.currentTimeMillis()-starttime));
starttime = System.currentTimeMillis();
for (long i = 0; i < 888888888l; i++) {
if (i <= 10) {
}
}
System.out.println(“Total time taken case2 “+(System.currentTimeMillis()-starttime));
starttime = System.currentTimeMillis();
for (long i = 0; i < 888888888l; i++) {
if (i == 10 || i < 10) {
}
}
System.out.println(“Total time taken case1 “+(System.currentTimeMillis()-starttime));
starttime = System.currentTimeMillis();
for (long i = 0; i < 888888888l; i++) {
if (i <= 10) {
}
}
System.out.println(“Total time taken case2 “+(System.currentTimeMillis()-starttime));
}
Total time taken case1 2343
Total time taken case2 1641
Total time taken case1 1625
Total time taken case2 1641
Moreover:
if you do 88888888l loops
instead of 888888888l
You obtain this result:
Total time taken case1 235
Total time taken case2 172
Total time taken case1 156
Total time taken case2 172
Do it again
Total time taken case1 250
Total time taken case2 157
Total time taken case1 171
Total time taken case2 157