Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36873
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: One code is aggressively optimised, while another not. Why? |
| Date | 2017-07-12 21:02 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ok6gru$8dl$1@gioia.aioe.org> (permalink) |
| References | <87lgnu5b9o.fsf@munus.decebal.nl> |
On 7/12/2017 4:07 AM, Cecil Westerhof wrote:
> 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?
Other have already commented on the specifics.
But let me add a few general comments.
Optimizers are pretty smart these days, but they are not human. They
recognize certain patterns and do certain optimizations on them.
For a real world application consisting of thousands or tens
of thousands of constructs, then the optimizer get a large part
of them right and the result is an overall good performance.
But if you focus on just a single construct, then it becomes
almost random if the optimizer get it right or not.
And that conclusion is not constant over time. Any time
you switch Java version, then the optimizer can work
the same or do a better job or do a worse job.
Testing an extremely unrealistic small program for
something that is known that it can change with any
new Java version is in general not very productive.
Unless you happen to do number crunching and you need
to optimize DAXPY, then there are most likely much
better usage of the time.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll 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