Speed of math function atan: comparison D and C++
J-S Caux
js at gmail.com
Mon Mar 5 18:39:21 UTC 2018
On Monday, 5 March 2018 at 09:48:49 UTC, Uknown wrote:
> Depending on your platform, the size of `double` could be
> different between C++ and D. Could you check that the size and
> precision are indeed the same?
> Also, benchmark method is just as important as benchmark code.
> Did you use DMD or LDC as the D compiler? In this case it
> shouldn't matter, but try with LDC if you haven't. Also ensure
> that you've used the right flags:
> `-release -inline -O`.
>
> If the D version is still slower, you could try using the C
> version of the function
> Simply change `import std.math: atan;` to `core.stdc.math:
> atan;` [0]
>
> [0]: https://dlang.org/phobos/core_stdc_math.html#.atan
Thanks all for the info.
I've tested these two very basic representative codes:
https://www.dropbox.com/s/b5o4i8h43qh1saf/test.cc?dl=0
https://www.dropbox.com/s/zsaikhdoyun3olk/test.d?dl=0
Results:
C++:
g++ (Apple LLVM version 7.3.0): 9.5 secs
g++ (GCC 7.1.0): 10.7 secs
D:
dmd : 35.5 secs
dmd -release -inline -O : 29.5 secs
ldc2 : 34.4 secs
ldc2 -release -O : 31.5 secs
But now: using the core.stdc.math atan as per Uknown's suggestion:
D:
dmd: 9 secs
dmd -release -inline -O : 6.8 secs
ldc2 : 10 secs
ldc2 -release -O : 6.5 secs <- best
So indeed the difference is between the `std.math atan` versus
the `core.stdc.math atan`. Thanks Uknown! Just knowing this trick
could make the difference between me and other scientists
switching over to D...
But now comes the question: can the D fundamental maths functions
be propped up to be as fast as the C ones?
More information about the Digitalmars-d-learn
mailing list