Java System get current time in millisecond for benchmark

Introduction

currentTimeMillis() returns the current time in milliseconds.

We can use it for benchmark.

public class Main {
    public static void main(String[] arguments) {
        long startTime = System.currentTimeMillis();
        long endTime = startTime + 60000;
        long index = 0;
        while (true) {
            double x = Math.sqrt(index);
            long now = System.currentTimeMillis();
            if (now > endTime) {
                break;
            }//from   w  w  w.  j  a v a  2  s.c  o m
            index++;
        }
        System.out.println(index + " loops in one minute.");
    }
}



PreviousNext

Related