Java Array Length: Get Size of Array

Use and benchmark the length int on arrays. Length is used for looping and accessing the last element.
Length of arrays. An array may be empty—it may have zero elements. But it still has an element count (a size). We access length, an int, on all array instances.
Loops, optimizations. Accessing length is fast. And in loops, using length directly, not a local variable, may lead to better performance.
Example program. Let us use an array's length in a simple context. We find that the array here has three elements. And its last index is derived from its length.

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
Length as loop boundary. This is a basic example, and you already know how to loop over a simple array. We benchmark similar code later in this tutorial.For
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
Performance, length versus local. Suppose we need to check the array length in a program many times, and it does not change. This is not in a loop.

Result: Checking a local variable that copies the value of an array's length is faster.

Be careful: This 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
Performance, length in loop. Now we see how using length in a for-loop boundary condition helps performance. First this benchmark allocates, and loops over, an array.

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
Array lengths are often used in programs. As an optimization, if the length is not used in a loop condition, it may be faster to store in a local variable.
But in loops, leave the length access directly within the loop statement. This enables certain optimizations that give a measurable boost to performance.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls