double x = 0; vs. double x = 0.0; ?

40 views
Skip to first unread message

Bob Carpenter

unread,
Aug 10, 2016, 8:23:07 AM8/10/16
to stan...@googlegroups.com
Does anyone know if

double x = 0;

is less efficient than

double x = 0.0;

There's an implicit cast to double in the first assginemtn, but I
don't know if this will be done statically or at run time. I know
I should look at the assembler output if someone can remind me how
to do that.

- Bob

Jiqiang Guo

unread,
Aug 10, 2016, 8:58:36 AM8/10/16
to stan...@googlegroups.com
only answers the last part of the question: use -S for (clan)g++.




- Bob

--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avraham Adler

unread,
Aug 10, 2016, 10:29:54 AM8/10/16
to stan development mailing list
On Wednesday, August 10, 2016 at 8:58:36 AM UTC-4, Jiqiang Guo wrote:
> only answers the last part of the question: use -S for (clan)g++.
>
>
> http://stackoverflow.com/questions/840321/how-can-i-see-the-assembly-code-for-a-c-program
>
>
>
>
>
> On Wed, Aug 10, 2016 at 8:23 AM, Bob Carpenter <ca...@alias-i.com> wrote:
> Does anyone know if
>
>
>
>   double x = 0;
>
>
>
> is less efficient than
>
>
>
>   double x = 0.0;
>
>
>
> There's an implicit cast to double in the first assginemtn, but I
>
> don't know if this will be done statically or at run time.  I know
>
> I should look at the assembler output if someone can remind me how
>
> to do that.
>
>
>
> - Bob


Looks like optimizations happen at assemble time as the assembly code looks identical whether or not 0 or 0.0 is passed to x. Source and assembly outputs below. the _X.s were assembled calling `g++ -S -masm=intel test_X.cpp -o test_X.s` and the _XO.s were assembled calling `g++ -march=native -pipe -O3 -S -masm=intel test_X.cpp -o test_XO.s`

Hope that helps.

Avi
test_D.cpp
test_D.s
test_DO.s
test_I.cpp
test_I.s
test_IO.s

Bob Carpenter

unread,
Aug 10, 2016, 11:36:52 AM8/10/16
to stan...@googlegroups.com
Thanks. That's good to know. It means I think we should
just use 0 in our programs rather than 0.0. But if others
think 0.0 is clearer (or safer against bad compilers), I'd
just like to be consistent throughout.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <test_D.cpp><test_D.s><test_DO.s><test_I.cpp><test_I.s><test_IO.s>

Andrew Gelman

unread,
Aug 10, 2016, 5:04:33 PM8/10/16
to stan...@googlegroups.com
I've been discouraging people from using 0.0 because it then can be taken to imply that this is necessary.
Reply all
Reply to author
Forward
0 new messages