Earlier quoted context omitted.
It's usually a good idea to turn loop bound into a variable when benchmarking a compiler, lest it optimizes the whole thing away like in this case.
So if the compiler is too good you want to trick it to produce less optimal code so you can benchmark it fairly? Isn't it part of the benchmark to allow the compiler reduce the whole expression to a compile time constant?
Why is 2 * (i * i) faster than 2 * i * i in Java?
31–40 of 109 posts
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#32Earlier quoted context omitted.
It's usually a good idea to turn loop bound into a variable when benchmarking a compiler, lest it optimizes the whole thing away like in this case.
Nope; doesn't work for clang. Clang actually detects and compiles the algebraic closed form sum(i^2, n) for a bound n.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#33So it's an issue of the optimizer; as is often the case, it unrolls too aggressively and shoots itself in the foot, all the while missing out on various other opportunities. In my experience, loop unrolling should basically never be done except in extremely degenerate cases; I remember not long ago someone I know who also optimises Asm remarking "it should've died along with the RISC fad". The original goal was to re…
> In my experience, loop unrolling should basically never be done except in extremely degenerate cases Not true. Like many such optimizations, loop unrolling can be useful because it makes downstream loads constant. For example: float identity[4][4]; for (unsigned y = 0; y In this case, the compiler probably wants to unroll the loops so that it can straightforwardly forward the constant matrix entries directly to the…
This is also how compilers do things, but it is only that we schemers can see the intermediate result much easier using simple source->source transformations.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#34Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#35Earlier quoted context omitted.
It is in the first example (the sal instruction)
However, if you look at the second, you won't see any left shifts, which is also interesting
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#36Earlier quoted context omitted.
It is a good answer, but my favorite by far is an answer about branch prediction to explain why processing a sorted array is faster than unsorted: https://stackoverflow.com/q/11227809/938695
I find it interesting that there are developers out there that know to look at these nuances when respond to Stack Overflow questions. I'm been developing professionally for 10 years and probably went over branch prediction in my computer architecture class in college (I'm guessing I did, if I didn't then I never encountered it at all!). The person who answered the multiple question dove into byte code...but also ans…
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#37Earlier quoted context omitted.
It is a good answer, but my favorite by far is an answer about branch prediction to explain why processing a sorted array is faster than unsorted: https://stackoverflow.com/q/11227809/938695
I find it interesting that there are developers out there that know to look at these nuances when respond to Stack Overflow questions. I'm been developing professionally for 10 years and probably went over branch prediction in my computer architecture class in college (I'm guessing I did, if I didn't then I never encountered it at all!). The person who answered the multiple question dove into byte code...but also ans…
He probably has actual experience with branch prediction. He probably dabbled or had experience with angular in other jobs (he worked at google apparently, so maybe there).
He'll most likely be stumped if you provide a graphic problem that a graphic designer with a few years of experience would solve in an instant, or an ML problem for a data scientist with similar experience.
That doesn't mean he isn't extremely smart. He most likely is (it takes a lot of brain to do these things), but the fact that you can't tell branch prediction problems even though you had some computer architecture class in the past is irrelevant.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#38Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#39That just might be the most dedicated answer I've ever seen on Stack Overflow.
Re: Why is 2 * (i * i) faster than 2 * i * i in Java?
#40That just might be the most dedicated answer I've ever seen on Stack Overflow.
https://codegolf.stackexchange.com/questions/11880/build-a-w...