Question
How can I measure how long a method takes to run in Java?
I want to record a method's execution time and understand whether Java has a built-in utility specifically for timing how long a task takes.
I have found many results about timer classes used for scheduling threads and tasks, but that is not what I need. I want to measure elapsed execution time for code like this:
public void doWork() {
// method logic here
}
What is the correct way to time this kind of method execution in Java?
Short Answer
By the end of this page, you will understand how to measure elapsed time in Java, when to use System.nanoTime() instead of System.currentTimeMillis(), how to wrap code you want to time, and what mistakes to avoid when doing simple performance measurements.
Concept
Measuring a method's execution time means recording a start time, running the method, then recording an end time and calculating the difference.
In Java, the most common built-in tools are:
System.nanoTime()for measuring elapsed time accuratelySystem.currentTimeMillis()for measuring wall-clock time in milliseconds
For timing how long code takes to run, System.nanoTime() is usually the better choice.
Why System.nanoTime() matters
System.nanoTime() is designed for measuring durations. It gives you a high-resolution time source that is appropriate for computing elapsed time between two points.
long start = System.nanoTime();
doWork();
long end = System.nanoTime();
long duration = end - start;
This does not mean the value is the current date or time in nanoseconds. It is only useful for comparing two values to get a duration.
Why not use Timer?
Java's Timer class is for to run later or repeatedly. It is not intended for measuring how long a method takes.
Mental Model
Think of method timing like using a stopwatch:
- press start right before the method runs
- press stop right after it finishes
- subtract the two readings
System.nanoTime() is that stopwatch.
A Timer in Java is more like an alarm clock. It helps you schedule something to happen later. It does not tell you how long something just took.
So the key idea is:
- stopwatch = measure duration
- alarm clock = schedule future work
Syntax and Examples
The basic pattern looks like this:
long start = System.nanoTime();
// code to measure
someMethod();
long end = System.nanoTime();
long durationNanos = end - start;
System.out.println("Duration: " + durationNanos + " ns");
Example: timing a method
public class Main {
public static void main(String[] args) {
long start = System.nanoTime();
doWork();
long end = System.nanoTime();
long durationNanos = end - start;
double durationMillis = durationNanos / 1_000_000.0;
System.out.println("Execution time: " + durationNanos + " ns");
System.out.println("Execution time: " + durationMillis + " ms");
}
{
( ; i < ; i++) {
Math.sqrt(i);
}
}
}
Step by Step Execution
Consider this example:
public class Main {
public static void main(String[] args) {
long start = System.nanoTime();
greet();
long end = System.nanoTime();
long duration = end - start;
System.out.println(duration);
}
public static void greet() {
System.out.println("Hello");
}
}
Step by step
-
long start = System.nanoTime();- Java reads the current high-resolution time source.
- Suppose it returns
5000000000.
-
greet();- The method runs.
- It prints
Hello.
-
long end = System.nanoTime();
Real World Use Cases
Here are common places where developers time code in Java:
Logging slow operations
long start = System.nanoTime();
loadUserProfile();
long duration = System.nanoTime() - start;
System.out.println("loadUserProfile took " + duration + " ns");
Useful for:
- database calls
- file reading
- API requests
- expensive calculations
Comparing two approaches
You may want to compare:
- loop vs stream
- one sorting strategy vs another
- cached result vs recalculated result
Monitoring background jobs
For batch jobs, imports, and report generation, timing helps answer:
- how long did the whole job take?
- which step is slow?
- did the latest change improve performance?
Measuring scripts and command-line tools
If you build a Java tool that processes files or data, timing helps estimate runtime and detect regressions.
Real Codebase Usage
In real projects, developers rarely scatter raw timing code everywhere. They often use simple patterns that keep code readable.
Pattern: measure around a block of work
long start = System.nanoTime();
processOrder(order);
long elapsed = System.nanoTime() - start;
logger.info("processOrder took {} ns", elapsed);
Pattern: early return still needs careful timing
If a method has guard clauses or early returns, measure around the method call from the outside, or use a try/finally block.
long start = System.nanoTime();
try {
validateAndSave(user);
} finally {
long elapsed = System.nanoTime() - start;
System.out.println("validateAndSave took " + elapsed + " ns");
}
Pattern: validation and error handling
Timing is often combined with validation and exception tracking.
long start = System.nanoTime();
try {
sendEmail(message);
} catch (Exception e) {
System.err.println( + e.getMessage());
} {
System.nanoTime() - start;
System.out.println( + elapsed + );
}
Common Mistakes
1. Using Timer instead of elapsed-time measurement
Timer schedules tasks. It does not measure method runtime.
Broken idea:
Timer timer = new Timer();
This is the wrong tool for execution timing.
2. Using System.currentTimeMillis() for precise benchmarking
currentTimeMillis() is okay for rough measurements, but it is not ideal for short durations.
Better:
long start = System.nanoTime();
// code
long end = System.nanoTime();
3. Forgetting to subtract start from end
Broken code:
long start = System.nanoTime();
doWork();
System.out.println(System.nanoTime());
This prints a raw time source value, not the duration.
Correct:
Comparisons
| Option | Purpose | Good for timing execution? | Notes |
|---|---|---|---|
System.nanoTime() | Measure elapsed time | Yes | Best choice for durations |
System.currentTimeMillis() | Get current wall-clock time | Sometimes | Fine for rough timing, less precise |
java.util.Timer | Schedule future tasks | No | Not for measuring runtime |
Thread.sleep() | Pause a thread | No | Not related to timing a method |
nanoTime() vs currentTimeMillis()
Cheat Sheet
Quick pattern
long start = System.nanoTime();
methodToMeasure();
long duration = System.nanoTime() - start;
Convert units
double ms = duration / 1_000_000.0;
double sec = duration / 1_000_000_000.0;
Prefer this
- Use
System.nanoTime()for elapsed time - Use
try/finallyif exceptions may happen - Run multiple times for better estimates
Avoid this
- Do not use
java.util.Timerto measure method duration - Do not rely on one run for serious benchmarking
- Do not include unrelated console output in the timed section
Reusable helper
public static long measure(Runnable task) {
long System.nanoTime();
task.run();
System.nanoTime() - start;
}
FAQ
How do I measure execution time of a method in Java?
Use System.nanoTime() before and after the method call, then subtract the start value from the end value.
Is there a built-in timer utility in Java for measuring method runtime?
There is no special Timer class for this purpose. The standard approach is using System.nanoTime().
Should I use System.currentTimeMillis() or System.nanoTime()?
Use System.nanoTime() for elapsed time measurement. Use currentTimeMillis() only for rough timing or clock-based values.
Why is java.util.Timer not the right tool?
java.util.Timer schedules tasks to run later or repeatedly. It does not measure how long code takes to execute.
Can I measure very fast methods accurately?
You can, but results may be noisy. Run the method many times and compare averages for a more useful measurement.
What if the method throws an exception?
Use a try/finally block so you still record the elapsed time even if the method fails.
Is nanoTime() actually returning the current time in nanoseconds?
Mini Project
Description
Build a small Java program that measures how long different tasks take to run. This project demonstrates practical elapsed-time measurement using System.nanoTime() and shows how to reuse timing logic for multiple methods.
Goal
Create a reusable method-timing utility and use it to measure at least two different tasks in Java.
Requirements
[ "Create a method that accepts a Runnable and returns the elapsed time.", "Measure the runtime of two different tasks.", "Print the durations in both nanoseconds and milliseconds.", "Use System.nanoTime() for all timing measurements." ]
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.