Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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
Advertisements