1
\$\begingroup\$

Here is my Timer class code, and the context it is being used in. How accurate is the timer for measure elapsed time?

public class P3 {

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.startStop();
        System.out.println(PrimeNumber.getLargestPrimeFactor(600851475143L));
        timer.startStop().printTime().reset();
        Timer timerTwo = new Timer();
        timerTwo.startStop();
        for(int i = 0; i < 100; i++) {
            timer.startStop();
            PrimeNumber.getLargestPrimeFactor(6008514751435143L);
            timer.reset();
        }
        timerTwo.startStop().printTime().reset();
        timer.printAverageTime();
    }


    public class Timer {

    private static int timers;
    private static int id;
    private long startTime;
    private long time;
    private long totalTime;
    private int numberOfResets;
    private boolean running;
    private TimeUnit unit;

    public Timer() {
        timers++;
        id = timers;
        this.unit = TimeUnit.MILLISECONDS;
    }

    public Timer(TimeUnit unit) {
        timers++;
        id = timers;
        this.unit = unit;
    }

    public static enum TimeUnit {
        NANOSECONDS(1.0), MILLISECONDS(1000000.0), SECONDS(1000000000), MINUTES(60000000000.0);
        private final double conversion;

        private TimeUnit(double conversion) {
            this.conversion = conversion;
        }

        public double convert(double time) {
            return time / this.conversion;
        }
    }

    public Timer startStop() {
        if (running) { // Stopping
            stop();
        } else { // Starting
            start();
        }
        return this;
    }

    private void start() {
        startTime = System.nanoTime();
        running = true;
    }

    private void stop() {
        time += System.nanoTime() - startTime;
        running = false;
    }

    public Timer reset() {
        if(running)stop();
        totalTime += time;
        numberOfResets++;
        time = 0;
        return this;
    }

    private long getTimePrivate() {
        if (running)
            return time + (int) (System.nanoTime() - startTime);
        else
            return time;
    }

    public double getTime() {
        return getTime(unit);
    }

    public double getTime(TimeUnit unit) {
        return unit.convert(getTimePrivate());
    }

    private double getAverageTimePrivate() {
        return ((double) totalTime) / numberOfResets;
    }

    public double getAverageTime() {
        return getAverageTime(unit);
    }

    public double getAverageTime(TimeUnit unit) {
        return unit.convert(getAverageTimePrivate());
    }

    public String toString() {
        return toString(unit);
    }

    public String toString(TimeUnit unit) {
        return Double.toString(unit.convert(time));
    }

    public Timer printTime() {
        printTime(unit);
        return this;
    }

    public Timer printTime(TimeUnit unit) {
        System.out.printf("Timer %d: %.3f %s%n", id, getTime(unit), unit.toString().toLowerCase());
        return this;
    }

    public Timer printAverageTime() {
        printAverageTime(unit);
        return this;
    }

    public Timer printAverageTime(TimeUnit unit) {
        System.out.printf("Timer %d average: %.3f %s%n", id, getAverageTime(unit), unit.toString().toLowerCase());
        return this;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Well, it's going to be as accurate as nanoTime at best case scenario, which is system dependent. That's not really a question we can answer. Additionally, this isn't really the right place to ask specific questions such as that. On CR, we review all aspects of the code. If you're more interested in a specific question you could ask on SO, but you'll get the answer I just mentioned. \$\endgroup\$ – Dan Pantry Aug 6 '16 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.