You are not logged in. Please login at www.codechef.com to post your questions!

×

# Performance difference this two technique of console output?

 0 I was solving one question based on printing pattern in which I had to perform lot of output stuff.I used to approach for this which are as follows:- APPROACH-1 I used System.out.println() statement for printing every single character on the screen. APPROACH-2 In this approach I appended every single character to a StringBuilder and then output that on screen using same System.out.println() statement. When I used first approach I got TLE then after seeing someone else's solution I used the second approach and got sufficiently higher performance, which not only pulled me out of TLE but also brought down my execution to the great extent. Can anyone explain me the reason why there is such a performance difference in the two approaches. asked 02 Dec '16, 08:54 2★arpit728 683●14●62 accept rate: 10%

 2 @arpit728 This is because System.out.println() flushes the output every time it is called, that's why it increases the running time of program, and it's a good habit to use StringBuilder so that you need to output once per test case. In StringBuilder it is not necessary to create new instance every time, after printing the output you can clear the data using method setLength(0). answered 02 Dec '16, 10:29 5★srd091 1.6k●1●11 accept rate: 42% 2 @srd091 I got you. What I did is I created the new instance of stringBuilder for every test case. wouldn't calling setLength(0) be overkill. (02 Dec '16, 17:05) arpit7282★ @arpit728 Yes, you don't need to create new instance for every test case. Instead of creating new instance for every test case you can create it once and then use setLength method to clear data at the end of each test case. (02 Dec '16, 17:27) srd0915★
 0 @arpit728 the answer provided by @srd091 is correct. However I just want to mention here that there is an alternative to using a StringBuffer which I prefer. You can wrap the standard output stream with a PrintWriter without autoflush to achieve the same result. PrintWriter pw = new PrintWriter(System.out); //by default autoflush is off pw.println("Hello, World!"); ... ... pw.flush(); //or pw.close() at the end of the program to flush the buffer  So the only changes to what you're used to doing is 1. Making a PrintWriter object at the beginning 2. Replacing all System.out.println() statements with pw.println() 3. Adding a pw.flush() at the end of the program This actually requires less typing if you make a personal template which you copy and use for every program ;) answered 02 Dec '16, 16:23 6★meooow ♦ 7.0k●7●17 accept rate: 48%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,253
×228
×60
×55
×4

question asked: 02 Dec '16, 08:54

question was seen: 798 times

last updated: 06 Dec '16, 00:10