Reputation: 808
I'm newbie Java learner. I'm trying to understand how can I write efficient codes in terms of both performance and readability. At this point arrays have been a puzzle for me. Six primitive tests are below. Their first three and second three return almost same times. Please explain what is going on.
String s= "";
int[] Array1=new int[100000];
Long startingTime=System.nanoTime();
for (int i = 0; i < Array1.length; i++) {
s+=i;
}
System.out.println("time : " + (System.nanoTime()-startingTime));
String s= "";
int length=100000;
Long startingTime=System.nanoTime();
for (int i = 0; i < length; i++) {
s+=i;
}
System.out.println("time : " + (System.nanoTime()-startingTime));
String s= "";
int length1=50000;
int length2=50000;
Long startingTime=System.nanoTime();
for (int i = 0; i < length1+length2; i++) {
s+=i;
}
System.out.println("time : " + (System.nanoTime()-startingTime));
public class Test3Length {
static class Foo {
int mSplat = 0;
}
public static void main(String[] args) {
int twentyMillions = 20000000;
Foo[] mArray = new Foo[twentyMillions];
for (int i = 0; i < mArray.length; i++) {
mArray[i] = new Foo();
}
int sum = 0;
Long startingTime = System.nanoTime();
for (int i = 0; i < mArray.length; ++i) {
sum += mArray[i].mSplat;
}
System.out.println("time : " + (System.nanoTime() - startingTime));
}
}
public class Test4Length {
static class Foo {
int mSplat = 0;
}
public static void main(String[] args) {
int twentyMillions = 20000000;
Foo[] mArray = new Foo[twentyMillions];
for (int i = 0; i < mArray.length; i++) {
mArray[i] = new Foo();
}
int sum = 0;
Long startingTime = System.nanoTime();
for (int i = 0; i < twentyMillions; ++i) {
sum += mArray[i].mSplat;
}
System.out.println("time : " + (System.nanoTime() - startingTime));
}
}
public class Test5Length {
static class Foo {
int mSplat = 0;
}
public static void main(String[] args) {
int twentyMillions = 20000000;
Foo[] mArray = new Foo[twentyMillions];
for (int i = 0; i < mArray.length; i++) {
mArray[i] = new Foo();
}
int sum = 0;
Long startingTime = System.nanoTime();
for (Foo a : mArray) {
sum += a.mSplat;
}
System.out.println("time : " + (System.nanoTime() - startingTime));
}
}
First question, would I prefer to int length
in for-loop condition rather than array.length
?
Second question, would I prefer a foreach
loop rather than for-loop unless array is collection?
Upvotes: 0
Views: 102
Reputation: 25855
First question, would I prefer to
int length
in for-loop condition rather thanarray.length
?
That literally makes no difference. array.length
is an invariant, so the JIT will only access it once anyway, making its value identical to a local variable in terms of performance.
Second question, would I prefer a
foreach
loop rather than for-loop unless array is collection?
When the object you loop over is an array, the translation of a foreach-loop is the same as a for-loop, so that makes no difference whatsoever.
However, in conclusion I should say that you seem to be focusing on the wrong things. In your first three tests, virtually all of your run-time is likely to be spent on the s+=i
expression, since each evaluation creates a new String
in order to add i
to it. You will probably see much more massive speed-ups by using a StringBuilder
instead of trying different variants of the looping construct.
Upvotes: 1
Reputation: 11973
In real life it does not really matter if you use foreach or for loop. The performance difference is barely noticible. If you need the current index then do for loop, if not then go with foreach. As for the length, it is just a property, and since it is an array it is set when the array is initialized and never change. Reading the value of a property takes almost no time at all. Thus for readability's sake put it inside for loop's condition
Upvotes: 3