C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Caution: If an array has zero elements, you cannot get the last index by subtracting one. We must check this case.
Java program that uses array length
public class Program {
public static void main(String[] args) {
String[] array = { "cat", "apple", "frog" };
// Display length of the array.
System.out.println(array.length);
// Display first and last elements.
System.out.println(array[0]);
System.out.println(array[array.length - 1]);
}
}
Output
3
cat
frog
Java program that uses length, loop
public class Program {
public static void main(String[] args) {
int[] numbers = { 5, 10, 15, 20 };
// Loop over numbers using length.
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output
5
10
15
20
Version 1: Here we access the array's length each time in a hot inner loop. The length never changes.
Version 2: In this version of the code we use a cached "length" local instead of accessing length each time.
Result: Checking a local variable that copies the value of an array's length is faster.
Warning: This optimization does not work in a loop boundary. The Java runtime applies special optimizations in loops.
Java program that tests array length in if-statement
public class Program {
public static void main(String[] args) {
int[] array = new int[1000];
int length = array.length;
long t1 = System.currentTimeMillis();
// Version 1: check array length.
for (int i = 0; i < 10000000; i++) {
for (int j = 0; j < 100; j++) {
if (array.length != 1000) {
System.out.println(false);
}
}
}
long t2 = System.currentTimeMillis();
// Version 2: check length cached value.
for (int i = 0; i < 10000000; i++) {
for (int j = 0; j < 100; j++) {
if (length != 1000) {
System.out.println(false);
}
}
}
long t3 = System.currentTimeMillis();
// ... Timings.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
14 ms, check array.length
9 ms, check local variable
Version 1: This code loops over a 100,000 element array and accesses array.length in the for-loop limit.
Version 2: This version uses a local variable instead of array.length. In the loop it accesses the array.
Result: The loop that accesses the array length directly is faster. The compiler is optimizing bounds-checking in the loop body.
Java program that tests array length in for-loop
public class Program {
public static void main(String[] args) {
int[] array = new int[100000];
int length = array.length;
// Access all elements in memory (prime the cache).
for (int v = 0; v < array.length; v++) {
if (array[v] != 0) {
System.out.println(false);
}
}
long t1 = System.currentTimeMillis();
// Version 1: use array length in for-loop maximum.
for (int i = 0; i < 30000000; i++) {
for (int j = 0; j < 100; j++) {
int count = 0;
for (int v = 0; v < array.length; v++) {
count++;
}
if (count == 0) {
System.out.println(0);
}
}
}
long t2 = System.currentTimeMillis();
// Version 2: use local variable as for-loop maximum.
for (int i = 0; i < 30000000; i++) {
for (int j = 0; j < 100; j++) {
int count = 0;
for (int v = 0; v < length; v++) {
count++;
}
if (count == 0) {
System.out.println(0);
}
}
}
long t3 = System.currentTimeMillis();
// ... Timings.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
3114 ms, loop with length max
6785 ms, loop with local variable max