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,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- 21 Useful Android Emulator Short-cut Keyboard Keys - Android
- Command to display epoch time Terminal - Linux
- Save a Notepad file as Microsoft Excel file - Microsoft
- Calculate Area of Parallelogram - C-Program
- Sharepoint Server 2016 installation Prerequisites with download links - SharePoint
- Spell check not working in Gmail : Mac OS X - Mac-OS-X
- How to kill tomcat server process using Mac Terminal Command - Tomcat
- How to Change Android Toast Position? - Android