Thursday, March 10, 2005

How to measure execution time

Introduction

This is how to make a simple stopwatch in Java.

Program

class Fib {
    public static void main(String[] args) {
        long start = System.currentTimeMillis(); // start timing
        System.out.println(fib(Integer.parseInt(args[0]))); // the code to time
        long stop = System.currentTimeMillis(); // stop timing
        System.out.println("TimeMillis: " + (stop - start)); // print execution time
    }
    
    static final long fib(final int n) {
        if (n <= 2)
            return 1;
        else
            return fib(n - 1) + fib(n - 2);
    }
}

This solution is simple, but not very OOP-ish. The code should be put inside a class, like this:

public class Stopwatch {
    private long start;
    private long stop;
    
    public void start() {
        start = System.currentTimeMillis(); // start timing
    }
    
    public void stop() {
        stop = System.currentTimeMillis(); // stop timing
    }
    
    public long elapsedTimeMillis() {
        return stop - start;
    }
    
    public String toString() {
        return "elapsedTimeMillis: " + Long.toString(elapsedTimeMillis()); // print execution time
    }
}

This is how you use the new Stopwatch class:

class Fib {
    public static void main(String[] args) {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.start();
        System.out.println(fib(Integer.parseInt(args[0]))); // the code to time
        stopwatch.stop();
        System.out.println(stopwatch);
    }
    
    public static final long fib(final int n) {
        if (n <= 2)
            return 1;
        else
            return fib(n - 1) + fib(n - 2);
    }
}

Discussion

The Stopwatch class is very simple and easy to use, and of course, you can make as many Stopwatch objects as you want. Method toString() is called automatically when you try to print out the object. The method represents the object textually. Notice that almost every method just contains one line of code. This is typical for OOP. The values start and stop is now protected against someone changing the values accidentally. This is also a typical OOP feature.

2 comments:

Kristian Rafteseth said...

extremely big code when you can do it in 2 lines.

long time = System.currentTimeMillis();
...
do your thing
...
System.out.println((System.currentTimeMillis()-time));

Gaute Lykkenborg said...

Your code is perfectly fine, so please continue to use it. However, I can not see that it is much shorter. Below are the two solutions stripped of any unnecessary characters. Solution 1 is somewhat longer for readability and clarity, and I think that is really important because I try to explain something with my code. Anyway, I would not call 94 characters of code “extremely big” compared to 86 characters. Also, 3 statements does not qualify as “extremely big code”, even if the second solution consists of “only” two statements. Finally, with the second solution, the result is lost as soon as it is printed out. In certain cases that could prove a problem.

Solution 1
long a=System.currentTimeMillis();long b=System.currentTimeMillis();System.out.println(b-a));

Solution 2
long a=System.currentTimeMillis();System.out.println((System.currentTimeMillis()-a));