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

已查看 40 次
跳至第一个未读帖子

Bob Carpenter

未读,
2016年8月10日 08:23:072016/8/10
收件人 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

未读,
2016年8月10日 08:58:362016/8/10
收件人 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

未读,
2016年8月10日 10:29:542016/8/10
收件人 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

未读,
2016年8月10日 11:36:522016/8/10
收件人 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

未读,
2016年8月10日 17:04:332016/8/10
收件人 stan...@googlegroups.com
I've been discouraging people from using 0.0 because it then can be taken to imply that this is necessary.
回复全部
回复作者
转发
0 个新帖子