TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

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.Array
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, loop boundary. This is a basic example, and you already know how to loop over a simple array. An array's length makes a good upper boundary for a loop.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
Benchmark, 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.

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
Benchmark, 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
A summary. 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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf