- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I time a method execution in Java
You should get a start time before making a call and end time after method execution. The difference is the time taken.
Example
import java.util.Calendar; public class Tester { public static void main(String[] args) { long startTime = Calendar.getInstance().getTimeInMillis(); longRunningMethod(); long endTime = Calendar.getInstance().getTimeInMillis(); System.out.println("Time taken: " + (endTime - startTime) + " ms"); } public static void longRunningMethod() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
Output
Time taken: 1012 ms
- Related Articles
- How to measure execution time for a Java method?
- How do I get time of a Python program's execution?
- How to calculate elapsed/execution time in Java?
- Calculate the execution time of a method in C#
- How do I write method names in Java?
- How do I invoke a Java method when given the method name as a string?
- How to find PHP execution time?
- How do I trigger a function when the time reaches a specific time in JavaScript?
- How to measure the execution time in Golang?
- Java Program to Calculate the Execution Time of Methods
- How do I empty a list in Java?
- How do I search a list in Java?
- How to calculate Execution Time of a Code Snippet in C++?
- How to improve the execution time of a query in MongoDB?
- How do we upsample a time-series using asfreq() method?

Advertisements