开发者

time required in java

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 woul开发者_如何转开发d 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)?


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.


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");


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.


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


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.)


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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜