Live data from Hacker News

Why is 2 * (i * i) faster than 2 * i * i in Java?

stackoverflow.com

41–50 of 109 posts

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#42
post #19

Earlier quoted context omitted.

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…

That person works in financial services, which I'm guessing is basically some form of automated trading. It is an industry where every cycle counts (so much so, that often times light speed latency between two edges is something you need to consider when placing servers). He probably has actual experience with branch prediction. He probably dabbled or had experience with angular in other jobs (he worked at google app…

The author of that answer wrote y-cruncher, which has been used to set world records in the number of digits of pi calculated. So I'm not surprised at all to see that they how know branch prediction works.

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#44

I don't see how generating different code for the same mathematical expression can be a good thing. The compiler should detect that the two expressions are strictly equivalent and generate whatever code it believes is the fastest. Any idea why it is this way?

Because it's more work for the compiler to reduce the expression to something canonical (and it might even be impossible).

Also what good will it bring? What if the canonical expression triggers the slow path? Now you have no means to change it into the fast version.

Further, in the case of floating point operations, operation order matters for rounding. And with integer operations, the actual form used can be important for preventing overflow (of intermediate results).

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#45

Earlier quoted context omitted.

Did you read TFA? The author did that (though using GCC), and the reason the optimizer does what you see is undefined behavior due to signed integer overflow.

> does what you see is undefined behavior Just to be clear, undefined behavior means the standard allows implementations to do what they they feel is the right thing to do under that scenario, and the outcome will still comply with the standard.

No, that's implementation defined behaviour.

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#46

Earlier quoted context omitted.

That person works in financial services, which I'm guessing is basically some form of automated trading. It is an industry where every cycle counts (so much so, that often times light speed latency between two edges is something you need to consider when placing servers). He probably has actual experience with branch prediction. He probably dabbled or had experience with angular in other jobs (he worked at google app…

The author of that answer wrote y-cruncher, which has been used to set world records in the number of digits of pi calculated. So I'm not surprised at all to see that they how know branch prediction works.

> they how know branch prediction works.

Can’t tell if clever joke, or typo

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#47

Earlier quoted context omitted.

The author of that answer wrote y-cruncher, which has been used to set world records in the number of digits of pi calculated. So I'm not surprised at all to see that they how know branch prediction works.

> they how know branch prediction works. Can’t tell if clever joke, or typo

Typo, but I'll leave it up to brighten someone else's day.

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#48

I don't see how generating different code for the same mathematical expression can be a good thing. The compiler should detect that the two expressions are strictly equivalent and generate whatever code it believes is the fastest. Any idea why it is this way?

Because of integer overflows and floating-point operations, the notion of equivalent mathematical expressions is tricky.

    fn main() {
        let a: i8 = 125;
        let b: i8 = 3;
        let c: i8 = (a + b) / 2;
        let d: i8 = b + ((a - b) / 2);
        println!("{} {}", c, d);
    }
This program outputs `-64 64` although the computations of `c` and `d` are equivalent.

Here's another example using floating point numbers:

    fn main() {
        let mut total1: f32 = 0.0;
        let mut total2: f32 = 0.0;
        let mut counter1: f32 = 0.0;
        let mut counter2: f32 = 100.0;

        for _ in 0 .. 10001 {
            total1 += counter1;
            total2 += counter2;
            counter1 += 0.01;
            counter2 -= 0.01;
        }
        println!("{} {}", total1, total2);
    }
The output of this program is `500041.16 500012.16`, a difference of 25 for a program that computes the same result (unless I made a mistake).

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#49

So 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…

Loop unrolling is useful only when it enables other optimisations. The most common is constant folding.

Re: Why is 2 * (i * i) faster than 2 * i * i in Java?

#50
post #19
post #9

Earlier 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…

I don't see anything regarding Angular, they're obviously very knowledgeable, but it's pretty much focused on low-level: https://stackoverflow.com/users/922184/mysticial?tab=tags&so...
Post reply on HN