8

Let's say I wanted to print 5 lines. Which is the best method (for performance and readability).

System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();

or

System.out.println("\n\n\n\n");

Is it a matter of preference or is one better than the other. It seems like it would save a lot of time using the second method.

Will
  • 19,661
  • 7
  • 47
  • 48
  • @pst -- I'd +2 your comment if I could :-) – Kal Jul 13 '11 at 21:08
  • I seriously doubt there's any difference in performance, and readability is a personal choice (and would depend on the situation). – Riaan Cornelius Jul 13 '11 at 21:10
  • 5
    I remember when I first started I imagined almost the same question. As a student it's difficult to know what real computer scientists would do. I remember feeling that "\n" looked very strange and always preferred extra printlns. This isn't a trivial question for someone just starting out in computer science. (@pst still +1, but I think you should explain why: that's a very interesting question!) – Ziggy Jul 13 '11 at 21:23

8 Answers8

31

There is a functional difference between the two. The first version outputs line breaks using the platform's preferred line separator. The second version outputs newline characters, which is likely to be inappropriate on Windows or Mac OS.

This is more important than any real or imagined performance advantages.


On the topic of performance, and why everyone seems to be saying "enough already".

The performance difference between your two ways of writing that code is likely to be a small number of microseconds, or less. In other words, an end user won't notice the difference ... unless the code is executed millions of times.

As a general rule, professional software engineers take the view that it is not worth spending time to make something faster if it doesn't need to be faster. And it is certainly not worth spending the client's money doing this.

You should only indulge in micro-optimization if you have clear evidence that there is, or will be a performance problem, and that the code that you are about to optimize is where the real problem is / will be. Time spent optimizing the wrong bit of code is time wasted.

So how do you know when to optimize?

  • When the application is observably slow when measured against criteria that actually matter.

And how do you know what to optimize?

  • By running application profilers, and analysing their output to see where the actual performance hotspots and bottlenecks are.

Performance is not always an unimportant issue. Indeed, for some kinds of software, a design or implementation that doesn't take account of performance and scalability requirements can be a total disaster. However, most software is not like that.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    While all your points are valid and valuable, learning to write performant code from the start means less need to optimize later. – MikeHall Jul 10 '15 at 17:18
  • 1
    @MikeHall - Maybe. But for most people, "writing performant code the first time" means taking longer to think about and write the first version of their code. (Not to mention, wasting time on asking questions like this one ... in the mistaken belief that it will make any difference.) Sure, there are exceptional programmers out there for whom writing performant code is easy. But they are exceptional ... and if they are really honest, even they will admit that it is not as easy as it seems. – Stephen C Oct 18 '15 at 07:23
9

Perhaps better than either:

System.out.printf("%n%n%n%n%n");

That uses the line separator appropriate to your platform, where "\n" doesn't.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • This is more concise than 4 calls to `println()`, but probably slower when you consider the overheads of decoding the format string. On the other hand, that *probably doesn't matter!!!* So this is a good alternative. – Stephen C Feb 01 '17 at 07:57
0

For platform dependent line separator I would use:

String ls = System.getProperty("line.separator");
System.out.print(ls+ls+ls+ls+ls);

That should work on win, linux or mac.

Pablo Pazos
  • 3,080
  • 29
  • 42
0

The 2nd one is definitely less code, performance wise you might not see a difference but the latter may be faster as it is only 1 call but it would be negligible.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

As you can see from the answers, this is a matter of preference. The example you give is a very simple one where the performance gains and readability issues are minimal. In some more complicated cases, however, the choice of which seemingly equivalent methods to use could be crippling to your code, or could make it indecipherable to a neighbor.

If code looks like it's getting kind of messy, try abstracting out the messy code and giving it an easy to read method name.

So yes, this is a matter of preference.

(Edit: no, this is apparently not a matter of preference (^o^ )// )

Ziggy
  • 21,845
  • 28
  • 75
  • 104
  • 2
    It's not just preference. As Stephen C pointed out there are *functional differences*. – Paul Cager Jul 13 '11 at 21:37
  • Hmm... if the user really is asking about println and is just a println fan who wants to know all about his favorite function, then yes that's the answer. I think, however, that the user is curious whether programmers prefer to repeat themselves or be terse. It's just a coincidence that in this case there is a functional difference (or perhaps the lesson here is that, given enough research any matter which was thought to be preference can be reduced to a functional choice?). – Ziggy Jul 13 '11 at 23:02
-1

what will be the output of System.out.println("\ /");

If the output is /, then what is reason behind that it is not giving the out \ /?

  • 1
    Welcome to StackOverflow! this answer does not provide a solution for the question, so it's recommended to ask your own question , in order to write what's the problem and what's the target, attach the portion of code interested and also explain what you've tried to solve the issues – xKobalt Jul 22 '20 at 07:34
-1

The second option is the better method.

It is not only easier for the programmer but results in less function overhead.

ssell
  • 6,429
  • 2
  • 34
  • 49
  • 4
    Are we still debating function overhead!? The hotspot optimizer will inline or optimize most things in such a way that unless we are talking about calling a method millions of time in a loop, the performance hit is negligible... – Riaan Cornelius Jul 13 '11 at 21:13
  • Yea, I failed to mention that the performance hit is non-existent in this example. – ssell Jul 13 '11 at 21:25
  • 2
    +1 but listen, how much you wanna bet that the original poster doesn't know about that fascinating topic. Why not post an answer to that effect? – Ziggy Jul 13 '11 at 21:26
-1

The second option is almost certainly faster because each call to println() causes a system function to be called. This jump to kernel code takes some amount of time. This is the reasoning behind buffering in in C.

Maz
  • 3,375
  • 1
  • 22
  • 27