Guest User

Untitled

a guest
Sep 9th, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. public class IncrementTest
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Stopper s = new Stopper();
  9.        
  10.         int a = 0;
  11.         int b = 0;
  12.  
  13.         List<Long> timer = new ArrayList<>();
  14.         List<Long> timer2 = new ArrayList<>();
  15.        
  16.         for(int o=0; o<1000000; o++){
  17.             s.startNano();
  18.             for(int i=0; i<Integer.MAX_VALUE; i++)
  19.                 ++a;
  20.             s.stopNano();
  21.             timer.add(s.getDiff());
  22.            
  23.             s.startNano();
  24.             for(int i=0; i<Integer.MAX_VALUE; i++)
  25.                 b++;
  26.             s.stopNano();
  27.             timer2.add(s.getDiff());
  28.         }
  29.        
  30.         double a1 = timer.stream().mapToLong(l -> (long) l).average().orElse(-1);
  31.         double a2 = timer2.stream().mapToLong(l -> (long) l).average().orElse(-1);
  32.        
  33.         System.out.printf("preincrement:%2f\npostincrement:%2f\n", a1, a2);
  34.     }
  35.    
  36. }
  37.  
  38. class Stopper{
  39.         private long start, end, diff;
  40.         public Stopper(){
  41.             start = 0;
  42.             end = 0;
  43.             diff = -1;
  44.         }
  45.         public void start(){
  46.             start = System.currentTimeMillis();
  47.         }
  48.         public void stop(){
  49.             end = System.currentTimeMillis();
  50.             diff = end - start;
  51.         }
  52.        
  53.         public void startNano(){
  54.             start = System.nanoTime();
  55.         }
  56.         public void stopNano(){
  57.             end = System.nanoTime();
  58.             diff = end - start;
  59.         }
  60.        
  61.         public long getDiff(){  return diff;    }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment