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.