Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: One code is aggressively optimised, while another not. Why? Date: Wed, 12 Jul 2017 10:07:15 +0200 Organization: Decebal Computing Lines: 43 Message-ID: <87lgnu5b9o.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="e8a517cbf8a434ef2a7722cda775d5e8"; logging-data="2776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CxVRm9rs8xX5tu2bg+ikoXq9A+CV1qcM=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:8MnhmT4T3Iu5tXx1tz9vT84/IDc= sha1:6GvuhzWH2llcLaqxZ+383tXAK0M= Xref: csiph.com comp.lang.java.programmer:36861 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