Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

System.out.println() vs System.out.print() speed

843789May 8 2009 — edited May 9 2009
I wasn't sure where to post this so here goes and Sorry if it was posted before

I was testing Java vs C speed an ran the Following Code:
WARNING Code does took about 5 min to run.
package a;

public class A {

	/**
	 * @param args
	 */
	static int j = 0;
	static int k = 0;
	static int l = 0;
	private static String[] test = new String[100000];
	private static float holder1[] = new float[100000];
	private static float holder2[] = new float[100000];
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
			for(int i = 0;i<100;i++)
			{
				withoutPrintln();
				withPrintln();
				System.out.println(i);
			}
				for(int i = 0; i<test.length;i++)
				{
					if(test[i] != null)
					System.out.println(test);
}
float total = 0;
float total2 = 0;
for(int i = 0;i<holder1.length;i++)
{
total+= holder1[i];
total2+=holder2[i];

}
System.out.println("Total with println " + total);
System.out.println("Total withoutPrintln " + total2);


}


private static void withPrintln()
{
long start = System.currentTimeMillis();

for(int i = 0; i <100000;i++)
System.out.println("a");
long elapsedTimeMillis = System.currentTimeMillis()-start;
float elapsedTimeSec = elapsedTimeMillis/1000F;
test[j]="Elapsed Time withPrintln" + elapsedTimeSec;
holder1[k] = elapsedTimeSec;
k++;
j++;
}
private static void withoutPrintln()
{
long start = System.currentTimeMillis();
for(int i = 0;i < 100000;i++)
{
System.out.print("a\n");

}
long elapsedTimeMillis = System.currentTimeMillis()-start;
float elapsedTimeSec = elapsedTimeMillis/1000F;
test[j] = "Elapsed Time withoutPrintln" + elapsedTimeSec;
holder2[l] = elapsedTimeSec;
l++;
j++;
}
}
Which prints a whole lot of a's using both System.out.println() & System.out.print("\n")

And i seem to always get that it is twice as fast to use System.out.print("\n");
Also, can somebody see if they get the same result on a non-Windows Computer.

Is the logic of my code wrong? 
Or does System.out.println(String s)
{
      system.out.print(s +"\n");
}

Causing overhead.

If this is true any Java vs C Speed test using println is unfair.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2009
Added on May 8 2009
10 comments
338 views