How to measure elapsed time

后端 未结 5 1821
暖寄归人
暖寄归人 2020-11-29 22:09

I have a 10 and 20 question game. I need to count how much time is passed when a user finishes the game.

Timer T=new Timer();
T.scheduleAtFixedRate(new Timer         


        
5条回答
  •  天命终不由人
    2020-11-29 23:10

    There are many ways to achieve this, but the most important consideration to measure elapsed time is to use System.nanoTime() and TimeUnit.NANOSECONDS as the time unit. Why should I do this? Well, it is because System.nanoTime() method returns a high-resolution time source, in nanoseconds since some reference point (i.e. Java Virtual Machine's start up).

    This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.

    For the same reason, it is recommended to avoid the use of the System.currentTimeMillis() method for measuring elapsed time. This method returns the wall-clock time, which may change based on many factors. This will be negative for your measurements.

    Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

    So here you have one solution based on the System.nanoTime() method, another one using Guava, and the final one Apache Commons Lang

    public class TimeBenchUtil
    {
        public static void main(String[] args) throws InterruptedException
        {
            stopWatch();
            stopWatchGuava();
            stopWatchApacheCommons();
        }
    
        public static void stopWatch() throws InterruptedException
        {
            long endTime, timeElapsed, startTime = System.nanoTime();
    
            /* ... the code being measured starts ... */
    
            // sleep for 5 seconds
            TimeUnit.SECONDS.sleep(5);
    
            /* ... the code being measured ends ... */
    
            endTime = System.nanoTime();
    
            // get difference of two nanoTime values
            timeElapsed = endTime - startTime;
    
            System.out.println("Execution time in nanoseconds   : " + timeElapsed);
        }
    
        public static void stopWatchGuava() throws InterruptedException
        {
            // Creates and starts a new stopwatch
            Stopwatch stopwatch = Stopwatch.createStarted();
    
            /* ... the code being measured starts ... */
    
            // sleep for 5 seconds
            TimeUnit.SECONDS.sleep(5);
            /* ... the code being measured ends ... */
    
            stopwatch.stop(); // optional
    
            // get elapsed time, expressed in milliseconds
            long timeElapsed = stopwatch.elapsed(TimeUnit.NANOSECONDS);
    
            System.out.println("Execution time in nanoseconds   : " + timeElapsed);
        }
    
        public static void stopWatchApacheCommons() throws InterruptedException
        {
            StopWatch stopwatch = new StopWatch();
            stopwatch.start();
    
            /* ... the code being measured starts ... */
    
            // sleep for 5 seconds
            TimeUnit.SECONDS.sleep(5);
    
            /* ... the code being measured ends ... */
    
            stopwatch.stop();    // Optional
    
            long timeElapsed = stopwatch.getNanoTime();
    
            System.out.println("Execution time in nanoseconds   : " + timeElapsed);
        }
    }
    

提交回复
热议问题