

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Calculate the Execution Time of Methods
In this article, we will understand how to calculate the execution time of methods. Time of execution is calculated by substracting End time and start time.
Below is a demonstration of the same −
Input
Suppose our input is −
Run the program
Output
The desired output would be −
The program is being executed: The Execution time of the program is: 620872 nanoseconds
Algorithm
Step 1 - START Step 2 - Declare 3 long values namely my_start_time, my_end_time and my_execution_time. Step 3 - Start time of the program is recorded using the function System.nanoTime() and assigned to variable my_start_time. Step 4 - Similarly end time of the program is recorded using the function System.nanoTime() and assigned to variable my_end_time Step 5 - Total execution time of the program is calculated by my_end_time - my_start_time. Store the value in my_execution_time. Step 6 - Display the result Step 7 - Stop
Example 1
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Main { public static void main(String[] args) { long my_start_time, my_end_time, my_execution_time; my_start_time = System.nanoTime(); System.out.println("The program is being executed:"); my_end_time = System.nanoTime(); my_execution_time = my_end_time - my_start_time; System.out.println("The Execution time of the program is: " + my_execution_time + " nanoseconds"); } }
Output
The program is being executed: The Execution time of the program is: 129621 nanoseconds
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Main { public static void main(String[] args) { long my_start_time, my_end_time, my_execution_time; my_start_time = System.nanoTime(); int my_input_1 = 100; int my_input_2 = 250; int my_sum = my_input_1 + my_input_2; System.out.println("The program is being executed:"); my_end_time = System.nanoTime(); my_execution_time = my_end_time - my_start_time; System.out.println("The Execution time of the program is: " + my_execution_time + " nanoseconds"); } }
Output
The program is being executed: The Execution time of the program is: 103801 nanoseconds
- Related Questions & Answers
- How to calculate elapsed/execution time in Java?
- Calculate the execution time of a method in C#
- Java Program to calculate the time of sorting an array
- How to calculate Execution Time of a Code Snippet in C++?
- PHP program to compute the execution time of a PHP script
- Methods to Calculate the Illumination of Light
- What is the order of execution of TestNG methods?
- Compilation and execution of Java Program
- Python Program to calculate the Round Trip Time (RTT)
- C Program to calculate the Round Trip Time (RTT)
- Java program to calculate the percentage
- How to measure execution time for a Java method?
- PHP program to calculate the total time given an array of times
- Java program to calculate the average of numbers in Java
- How to measure the execution time in Golang?
Advertisements