Modern C++ Lamentations

Walter Bright newshound2 at digitalmars.com
Mon Dec 31 18:43:46 UTC 2018


On 12/31/2018 8:05 AM, Steven Schveighoffer wrote:
> And the answer was provided by someone who examined the compiler output:
> 
> https://www.reddit.com/r/programming/comments/ab71ag/comparing_pythagorean_triples_in_c_d_and_rust/ecy6rqn/

For reference, the reddit comment is:

"Looking at the generated code in compiler explorer, it looks like the Rust 
compiler is not hoisting the multiplications out of the loops, while the C++ 
compiler (I used clang) does. Furthermore, it seems like using `for x in y..=z` 
etc results in quite convoluted conditions.
This code seems to perform the same as the C++: https://godbolt.org/z/-nzALh
It looks like there's some things to fix in the rust compiler.."



> And it fixes the problem with Timon's D version as well, new time down to 172ms 


One thing to consider is optimizers are designed by looking at the code 
generated by popular programming languages and popular programming methods. 
Hence the traditional loop structure gets a heluva lot of attention. The range 
version is fairly new, and so it will likely do less well.

 > (see my reply to that post).

Your reply is:

"Ooh, that's interesting. Same issue with the D version.
I had to work a bit on it, but this does work and is 172ms vs the ~1000ms:

   return
     recurrence!"a[n-1]+1"(1)
     .then!((z) {
        auto ztotal = z * z;
        return iota(1, z + 1).then!((x) {
            auto xtotal = x * x;
            return iota(x, z + 1)
               .filter!(y => y * y + xtotal == total)
               .map!(y => tuple(x,y,z));
               });
        });
"

Atila, can you please update the blog post?


More information about the Digitalmars-d mailing list