I have to iterate with a for-loop
, but I want to know which way is better.
1:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0,size=tmpList.size(); i < size; i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
2:
import java.util.ArrayList;
public class FindTime {
public static void main(String[] args) {
ArrayList tmpList = new ArrayList();
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
tmpList.add("me"); tmpList.add("you ");
tmpList.add("I"); tmpList.add("Us");
long startTime = System.nanoTime();
for (int i = 0;i < tmpList.size(); i++) {
System.out.println(tmpList.get(i).toString());
}
long endTime = System.nanoTime();
System.out.println("Time Duration ::->" + (endTime - startTime));
}
}
In the above, both for-loops
have the same content but just a difference in the loop condition. Can anyone tell me what is actually happening in the above iterations?
You are getting this all wrong. You are focusing on aspects that almost don't matter; and doing so, you wrote bad code - in both your examples!
First of all, you don't use raw types.
You go for this instead:
List<String> theWords = new ArrayList<>();
( Please note: it is also better practice to use List as actual type of your list object. No need to expose the implementation behind it to users of that list )
And Java has nice ways of "filling" a list, like
List<String> theWords = Arrays.asList("me", "didn't", "know", "that", "but", "could", "have");
Then; you use for-each to iterate all kinds of collections/arrays:
for (String aWord : theWords)
and you stop worrying about all these low level for loops with ints and increments and stuff.
In other words: Java is not C. We have better abstractions in many places; you better focus on those; as they take care of such subtleties. Meaning: focus on writing "higher level" code - because that creates readable code. And be assured: if you follow the typical "best practices" (as iterating loops using 'for each' like above) - you don't need to worry about performance. Because the JVM and JIT are best at optimizing things ... if you use the abstractions that they offer to you!
Trying to be smart; and expressing things "low level" can even have negative effects; because it might prevent the JIT from doing its glory optimization work (probably not in this case).
1 is faster because tmpList.size() is called only once.
2 is cleaner and easier to read because its simpler.
Cleanest
The following is cleanest because its easy to read and has full syntax highlight support in every IDE:
for ( String s : tmpList ) {
System.out.println(s);
}
Fastest
The following is the fastest performance because it uses a native array and avoids unnessesary object creation and getter method calls:
String[] tmpList = String[24];
tmpList[0] = "me";
tmpList[1] = "you";
...
for ( int i = 0; i < tmpList.length; ++i ) {
System.out.println(tmpList[i]);
}
Very Fastest
In fact the very fastest performance solution is to unroll your loop if you have a list which is known at compile time.
System.out.println("me");
System.out.println("you");
...
In general KISS (keep it simple stupid) and optimize only when you face a real performance problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With