LapisSea

Java timer/benchmarker

May 8th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. public class NanoTimer{
  2.    
  3.     private long    start;
  4.     private long[]  data    =new long[100];
  5.     private int     pos     =99,count;
  6.     private boolean started =false;
  7.    
  8.     public void start(){
  9.         started=true;
  10.         start=now();
  11.     }
  12.    
  13.     public void end(){
  14.         if(!started) throw new IllegalStateException("Not started");
  15.         pos=(pos+1)%100;
  16.         data[pos]=now()-start;
  17.         if(count<100) count++;
  18.        
  19.     }
  20.    
  21.     public double msAvrg100(){
  22.         long sum=0;
  23.         for(int i=0;i<count;i++){
  24.             sum+=data[i];
  25.         }
  26.         return toMs(sum/count);
  27.     }
  28.    
  29.     public double ms(){
  30.         return toMs(data[pos]);
  31.     }
  32.    
  33.     private static long now(){
  34.         return System.nanoTime();
  35.     }
  36.    
  37.     private static double toMs(long nano){
  38.         return Math.round(nano/10000D)/100D;
  39.     }
  40.    
  41. }
Add Comment
Please, Sign In to add comment