Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36861

One code is aggressively optimised, while another not. Why?

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject One code is aggressively optimised, while another not. Why?
Date 2017-07-12 10:07 +0200
Organization Decebal Computing
Message-ID <87lgnu5b9o.fsf@munus.decebal.nl> (permalink)

Show all headers | View raw


I am looking into some performance metrics. I have the following code:
    long sum = 0L;

    for (long i = start; i < end; ++i) {
        sum += i;
    }

And this code:
    long sum = 0L;

    for (int i = start; i < end; ++i) {
        sum += i;
    }

Start and end are defined like:
    private static int end   = Integer.MAX_VALUE;
    private static int start = Integer.MIN_VALUE;


The second code takes about 50% more time to execute. No surprise
there. But the first code is aggressively optimised when run several
times (the fifth run is about 2½ million times faster as the first
run), while the second code is not optimised:
    09:41:10: Timing long int
    09:41:10: took 2.08E+00 seconds.
    09:41:13: took 1.29E-03 seconds.
    09:41:13: took 1.25E-04 seconds.
    09:41:13: took 1.36E-06 seconds.
    09:41:13: took 7.97E-07 seconds.

    09:41:13: Timing long long
    09:41:13: took 3.09E+00 seconds.
    09:41:16: took 3.11E+00 seconds.
    09:41:19: took 4.03E+00 seconds.
    09:41:23: took 3.94E+00 seconds.
    09:41:27: took 3.87E+00 seconds.

Why the difference?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

One code is aggressively optimised, while another not. Why? Cecil Westerhof <Cecil@decebal.nl> - 2017-07-12 10:07 +0200
  Re: One code is aggressively optimised, while another not. Why? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-12 09:38 -0400
    Re: One code is aggressively optimised, while another not. Why? Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 09:51 +0200
      Re: One code is aggressively optimised, while another not. Why? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-07-13 11:05 -0400
        Re: One code is aggressively optimised, while another not. Why? Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 18:52 +0200
          Re: One code is aggressively optimised, while another not. Why? Cecil Westerhof <Cecil@decebal.nl> - 2017-07-13 19:51 +0200
  Re: One code is aggressively optimised, while another not. Why? Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2017-07-12 09:19 -0500
    Re: One code is aggressively optimised, while another not. Why? Martin Gregorie <martin@address-in-sig.invalid> - 2017-07-12 17:04 +0000
  Re: One code is aggressively optimised, while another not. Why? Arne Vajhøj <arne@vajhoej.dk> - 2017-07-12 21:02 -0400

csiph-web