If you want to know how much time did your program took to execute a program or code then you can make use of the following methods from the System class.
- System.currentTimeMillis()
- System.nanoTime()
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String... args) {
long timeMilli1 = System.currentTimeMillis();
myFunc();
long timeMilli2 = System.currentTimeMillis();
System.out.println("Time taken for the code to execute: " + (timeMilli2 - timeMilli1) + " milliseconds");
}
public static void myFunc() {
List mylist= new ArrayList<>();
for(int i=0;i<=1000;i++) {
mylist.add("Hello! :"+i);
}
}
}
Output:
Time taken for the code to execute: 19 milliseconds
Example 2: Calculate the time taken to execute code in nanoseconds
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String... args) {
long timeNano1 = System.nanoTime();
myFunc();
long timeNano2 = System.nanoTime();
System.out.println("Time taken for the code to execute: " + (timeNano2 - timeNano1) + " nanoseconds");
}
public static void myFunc() {
List mylist= new ArrayList<>();
for(int i=0;i<=1000;i++) {
mylist.add("Hello! :"+i);
}
}
}
Output:
Time taken for the code to execute: 19247334 milliseconds
More Posts related to Java,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations - Android
- PHP Script to Upload Images to Server - PHP
- Cannot open or preview pdf with view only and restricted download access in Microsoft Teams - Teams
- Android: Unknown error code during application install : - Android
- What is Bootstrap Jumbotron and how to use it - Bootstrap
- How to configure PDF iFilter for SharePoint - SharePoint
- Microsoft Sign-in Error Code: 50058 (Request Id, Correlation Id and Timestamp) - Microsoft
- How to exclude results from SharePoint Search - SharePoint