0

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?

4
  • 3
    Worry about readability, not efficiency; Premature optimization is the root of all evil. Commented Mar 3, 2015 at 22:28
  • 1
    @azurefrog: How do you know this optimization is premature? Perhaps this is code that OP has properly profiled and identified as a bottle-neck. There are times where performance matters, and learning to bring it out is certainly important. Commented Mar 3, 2015 at 22:57
  • @Dolda2000 Since the OP's question starts with "I'm newbie Java learner.", I assumed that not to be the case. Commented Mar 3, 2015 at 23:06
  • 1
    @azurefrog: Sure, but my main point was that I don't think it hurts to learn what language constructs perform better or worse, even early on. Just because you're trying to learn about performance doesn't by necessity mean that you'll be applying it wrong. Commented Mar 3, 2015 at 23:07

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

First question, would I prefer to int length in for-loop condition rather than array.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.

2 Comments

Thanks for answer and comments. You are right, s+=i usage is very bad practice.But I intentionally used it in my primitive test just for a longer time. Can you suggest me sources about performance issues? Thanks again. @Dolda2000.
@PhotonPoint: Sure, but that still means that whatever loop you use will consume a miniscule amount of the total run-time, so even then it's a bad way to test the different loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.