

- 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
How to measure the time taken by a function in Java?
java.lang.System.currentTimeMillis() method can be used to compute the time taken by a function in java. Trick is simple. Get the before time and after time using currentTimeMillis() where before time is the time when method is invoked and after time is when method has executed. See the example below −
Example
public class Tester{ public static void main(String[] args) { long startTime = System.currentTimeMillis(); longRunningMethod(); long endTime = System.currentTimeMillis(); System.out.println("Time taken: " + (endTime -startTime) + " ms"); } private static void longRunningMethod() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
Output
Time taken: 1000 ms
- Related Questions & Answers
- How to measure time taken by a function in C?
- How to measure elapsed time in Java?
- How to measure execution time for a Java method?
- Problem: Time taken by tomatoes to rot in JavaScript
- How to measure elapsed time in nanoseconds with Java?
- How to measure the execution time in Golang?
- Time taken by savepoint to perform backup in SAP HANA
- How to measure elapsed time in python?
- How to measure actual MySQL query time?
- How to obtain the time taken for a method to be executed in TestNG?
- How to measure time with high-precision in Python?
- Calculating time taken to type words in JavaScript
- C++ code to find the area taken by boxes in a container
- Actions taken by a kernel to context-switch between processes
- Find time taken for signal to reach all positions in a string - C++
Advertisements