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: Re: One code is aggressively optimised, while another not. Why? Date: Thu, 13 Jul 2017 18:52:25 +0200 Organization: Decebal Computing Lines: 52 Message-ID: <87a84846uu.fsf@munus.decebal.nl> References: <87lgnu5b9o.fsf@munus.decebal.nl> <87h8yg6ag8.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="cea988b749795bfe5e66959d383985c6"; logging-data="31281"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BcHLUHe4crCjLl7L/FPHojj0m/5+2Bwk=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:NcTj/Rs+CIGJ/kZkTcKwE3no11k= sha1:6/x+ox1+T4/vM9lf+siJnd1Ws8Y= Xref: csiph.com comp.lang.java.programmer:36901 Eric Sosman writes: > Remark: What's the point of all those Threads? The code > never starts even one of them, never uses any of them for > anything at all. I use the Threads as parameter to the test function. > Explanation/Rationale: Each test runs a loop to compute a sum, > but the sum is never subsequently used for anything at all: It is > not printed, it is not tested, it participates in no further > computation of any kind. The JVM may well notice this and decide > not to bother computing the sum since it isn't needed. A loop > that starts out as > > long sum = 0; > for (int i = start; i < end; ++i) { > sum += i; > } > > ... may then be transformed to > > for (int i = start; i < end; ++i) { > } > > ... which (since the loop has no effect on anything except `i', > which is itself not used outside the loop) may be further > optimized to > > { > } > > ... which casts just a wee bit of suspicion on the timings ;). OK, I change the code to return the sum. > I took the liberty of rewriting your test to eliminate the > Threads, to print the calculated sums (restraining the JVM's > eagerness to optimize), and to run a warm-up phase before the > "in earnest" loops. I'd be happy to mail you the code if you'd > like; maybe you can find as many "improvements" to mine as I did > to yours. I would appreciate it when you could email the code: I can only learn from it. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof