views:

220

answers:

9

i would like to know how much time is required to execute the conditional loops individually. Like if i had an option to use "if else", "while","for", or "foreach" loop then which loop would get executed faster.I know the difference would be very small and most would say that it would not matter but if i were to have a program which would access thousands of data then this point would come into picture.

I would like to know if i declare a variable in the beginning in java and if i were to declare it just before i use it will it make any difference. will the total time required be reduced? if yes then which is practically used (the one in which variables are declared in the beginning or where they are just declared b4 they are used)?

+14  A: 

Stop micro-optimizing.

Spend your time on finding the best algorithms, the best data structures, and the best design for your system, and writing the most readable code. Not on these little things.

polygenelubricants
I agree. Also, the VM will do most of that work.
mikek
+1 - the JIT compiler can do a better and more consistent job of optimizing than you can. Indeed, Sun folks advise that "clever" hand optimizing can actually make it harder for the JIT optimizer to do its job.
Stephen C
Agree. These little thins can change from version to version (just like with String/StringBuffer/StringBuilder). Use code that 'looks better'.
Michał Niklas
It helps to remember that it just plain _isn't your job_ as a programmer to do these kinds of nano-optimizations. You don't have the level of control required to do it. It's the VM's job.
Kevin Bourrillion
+7  A: 

There is not much point trying to hand-optimize your code - the JIT compiler is likely to do a lot better job than you. Not the least because it will have much more information about the exact environment at the point of execution than you can ever have before compilation.

Moreover, since optimizers are tuned for the "common case", introducing uncommon optimizations to your code may actually make it less optimizable by the JIT compiler, thus slower in the end.

Nevertheless, if you still really want to measure something, you can do it like this:

long before = System.currentTimeMillis();
// execute your code e.g. a million times
long after = System.currentTimeMillis();
System.out.println("Execution took " after - before " milliseconds");
Péter Török
+1 System.nanoTime() should be used in preference to System.currentTimeMillis btw.
Helper Method
@Helper Method, good point, thanks.
Péter Török
A: 

Why don't you find out yourself? Write a few smAll programs with large loops and do your measurements!

Raj More
If the OP were to do that, he would almost certainly come up with the wrong results. See http://www.ibm.com/developerworks/java/library/j-jtp02225.html for examples and reasoning. Microbenchmarks are hard in the era of Hotspot.
Andrzej Doyle
@Andrzej Doyle hero points for you.
Kevin Bourrillion
A: 

The difference will be in microseconds, implies, thousands of records means difference in milliseconds.

Spend your time on thinking why I will need to read/process thousands of records in the first place?

medopal
+2  A: 

It's impossible to answer in the general case, because of all the optimisations that Hotspot may or may not make to your code.

The important thing, as polygenelubricants says, is to write clear concise code - not only will this be easier for humans to understand (arguably the most important thing!) but it will be easier for optimising compilers to "understand" and turn into efficient code, too.

if you're having performance problems, then it's time to crack out a profiler, see where your code is actually spending its time and optimise that section. Again, this will usually involve improving an algorithm rather than using a marginally different primitive.

Andrzej Doyle
A: 

I agree with @polygenelubricants and @Péter Török.

For educational purposes though, you could compare the compiled code for the different types of loops using the javap command:

$ javap -v YourClass
aioobe
While educational, bytecodes aren't the most telling in comparing performance because the real optimization happens at JIT level.
polygenelubricants
+2  A: 

The difference will be very small, if they are relevant at all :-)

These days it is much more important to write clear, maintainable code than thinking of the difference between foreach or while loops.

The javac compiler will optimize the resulting bytecode, so there should not be any difference. Also its very specific which algorithms you use.

If you declare your variable in the beginning (? whats the "beginning") it will maybe have influence of the visibility, but likely not on the performance of your application.

Anyway, if you are interested in such performance optimization, have a look at the Java VM whitepapers from Sun: http://java.sun.com/docs/performance/

Also you maybe want to have a look at "profiling" your java applications, so you can see the difference by yourself. Here is a list of Open Source Java Profiling Tools: http://java-source.net/open-source/profilers

echox
+2  A: 

Let's be quite clear. You're micro-optimizing here. It's a waste of time. Here are the rules for optimization:

  1. Don't do it.
  2. (For experts only!) Don't do it yet.

Write to code to use good algorithms. Write the code to be clear. Check whether you still have a problem. Measure to find out where it is that the problem actually exists. Only optimize then, and only if you must. (And be aware that it can be exceptionally difficult to get good timing figures on modern computers, especially if you have non-trivial other things in memory such as an IDE.)

Donal Fellows
A: 

System.nanoTime

Apurva