Guest User

Untitled

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