Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class IncrementTest
- {
- public static void main(String[] args)
- {
- final int loopTimes = 1000000;
- Stopper s = new Stopper();
- int a = 0;
- long[] timer = new long[loopTimes];
- for(int o=0; o<loopTimes; o++){
- s.startNano();
- for(int i=0; i<Integer.MAX_VALUE; i++)
- ++a;
- s.stopNano();
- timer[o] = s.getDiff();
- }
- new Thread(() -> {
- int b = 0;
- long[] timer2 = new long[loopTimes];
- for(int o=0; o<loopTimes; o++){
- s.startNano();
- for(int i=0; i<Integer.MAX_VALUE; i++)
- b++;
- s.stopNano();
- timer2[o] = s.getDiff();
- }
- double a2 = Arrays.stream(timer2).average().orElse(-1);
- System.out.println("postincrement: " + a2);
- }).start();
- double a1 = Arrays.stream(timer).average().orElse(-1);
- System.out.println("preincrement: " + a1);
- }
- }
- class Stopper{
- private long start, end, diff;
- public Stopper(){
- start = 0;
- end = 0;
- diff = -1;
- }
- public void start(){
- start = System.currentTimeMillis();
- }
- public void stop(){
- end = System.currentTimeMillis();
- diff = end - start;
- }
- public void startNano(){
- start = System.nanoTime();
- }
- public void stopNano(){
- end = System.nanoTime();
- diff = end - start;
- }
- public long getDiff(){ return diff; }
- }
Advertisement
Add Comment
Please, Sign In to add comment