Pre increment operator is slightly performing better than inline and post increment operator

    public void postIncrementCost(){
        LocalTime pre = LocalTime.now();
        long start = System.currentTimeMillis();
        System.out.println("start postIncremental @==> "+ pre);
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                x++;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end postIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("postIncremental consumed @==&gt; "+ (end - start));
    }

  
  public void preIncrementCost(){
        LocalTime pre = LocalTime.now();
        System.out.println("start preIncremental @==&gt; "+ pre);
        long start = System.currentTimeMillis();
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                ++x;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end preIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("preIncremental consumed @==&gt; "+ (end - start));
    }

 
   public void mathIncrementCost(){
        LocalTime pre = LocalTime.now();
        System.out.println("start mathIncremental @==&gt; "+ pre);
        long start = System.currentTimeMillis();
        int x = Integer.MIN_VALUE;
        for(int i=0; i< Integer.MAX_VALUE; i++) {
            while (x < Integer.MAX_VALUE) {
                x = x+1;
            }
            x = Integer.MIN_VALUE;
        }
        LocalTime post = LocalTime.now();
        System.out.println("end mathIncremental @==&gt; "+ post);
        long end = System.currentTimeMillis();
        System.out.println("mathIncremental consumed @==&gt; "+ (end - start));
    }

start postIncremental @==> 15:47:41.764
end postIncremental @==> 15:47:41.773
postIncremental consumed @==> 9
start preIncremental @==> 15:47:41.774
end preIncremental @==> 15:47:41.782
preIncremental consumed @==> 8
start mathIncremental @==> 15:47:41.782
end mathIncremental @==> 15:47:41.791
mathIncremental consumed @==> 9

Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s