1

I take the following example to illustrate example but note that it could be any other task.

for (int i =0; i< 1000; i++){     
  String a= "world";
  Log.d("hello",a);
}

Versus

String a="";
a="world";
Log.d("hello",a);

String a="";
a="world";
Log.d("hello",a);

String a="";    
a="world";
Log.d("hello",a);

String a="";
a="world";
Log.d("hello",a);

String a="";    
a="world";
Log.d("hello",a);
...
//1000 times

Let's ignore readability and code quality, just performance of the compiled program. So which one is better?

TSR
  • 17,242
  • 27
  • 93
  • 197
  • The comprehensive answer is: it depends. – biziclop Dec 01 '16 at 11:27
  • 2
    Yes, but not compared to IO. – harold Dec 01 '16 at 11:28
  • If you want to know, test it out. Leave micro-optimizations to the compiler and the runtime. Don't copy&paste code like this because you believe that it performs better. Besides that, the performance difference in practice most likely doesn't matter. – Jesper Dec 01 '16 at 11:30
  • Very error prone, how can you easily verify that you have 1000 log items when you scroll through the code? Also you might even hit the method size limit http://stackoverflow.com/questions/17422480/maximum-size-of-a-method-in-java-7-and-8 – Viktor Mellgren Dec 01 '16 at 11:43
  • It's quite remarkable how people who have just learned to write some code are so worried about the speed of their code, even though the kind of code they're writing is usually very trivial and doesn't really matter even if it took twice the time needed. It's like starting to build a skyscraper and spending hours thinking what kind of antenna should be sitting on the top. – Kayaman Dec 01 '16 at 12:13

4 Answers4

5

The one and only answer: this doesn't matter at all.

What I mean is: aspects such as readability and a good design are much more worth spending your time on.

When, and only when you managed to design a superb application; and you are down to the point that you have a performance problem and you would have to look into this specific question; well, then there would be merit in looking into it. But I am 99.9% sure: you are not at this point. And in that sense: you are wasting your time with such thoughts!

In other words: especially within the "JVM stack" there are tons and tons of things that can have subtle or not-so-subtle effects on performance. Like: there are many many options that influence the inner works of garbage collection and what the Just-in-time compiler is doing. That will affect the "performance" of your running application in much more significant ways than manually un-rolling loop code.

And for the record: many benchmarking experiments showed that the JIT does do its best job if you give "normal" code to it. Meaning: the Oracle JIT is designed to give you "best results" on "normal" input. But as soon as you start to fiddle around with your java code, in order to somehow create "optimized" code ... chances are that your changes make it harder for the JIT to do a good job.

Thus: please forget about such micro-optimizations. If you spend the same amount of time learning about "good OO design" and maybe "generic java performance topics" ... you will gain much more from that!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

So which one is better?

The first one, since it actually compiles (there's a typo in the second). Non-compiling code is absolutely the least performant code.

But also, the first one, because, despite you saying "ignore readability and code quality", readability is almost always more important than performance. Readability is certainly what you should write code to be first, before making it performant.

And the first one, because the JIT will unroll the loop for you, it determines that has better performance on the specific JVM on which it is running, for the specific data that the code is run on (I'm assuming you don't simply like filling your logs with the same message over and over).

Don't try to second guess the optimizations the JIT will apply. Applying the optimizations yourself might make performance worse, because it makes it harder for the JIT to optimize the code. Write clear, maintainable code, and let the JVM work its magic.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Depending on the programming language. The compiler is responsible for compressing your code in such a way that it can avoid repeat calls at the low level. Object Orientated Programming with languages like java-script sit on a relatively high level, and therefore the compilation may take some time to run.

So to conclude briefly based widely on assumption from your question, the only impacts are the overall filesize and time taken to compile.

WizzKidd
  • 196
  • 8
0

What you are doing is called "Loop Unrolling".

Check: https://en.wikipedia.org/wiki/Loop_unrolling

This is one of the optimization which compilers will do if compiled with appropriate optimization level.

In my view your second option should run faster than first one. Because in first option code has to increment loop counter, check it against loop condition and take appropriate branch decision. Branching itself can be expensive if there is a branch mis-prediction. This is because on branch mis-prediction CPUs has to clear all the pipline and restart the instruction. Check: https://en.wikipedia.org/wiki/Branch_misprediction

kronus
  • 25
  • 5