if表达的表现或其他差异? [英] Performance or other differences on if expression?
问题描述
这两个之间有什么区别吗?
int i = f();
if(i)
和
int i;
if(i = f())
似乎如果我在
初始化定义,那么后者会保存作业吗?它是否适用于类类型或其他类型的b $ b类型?
" jeffc" <无**** @ nowhere.com>在消息中写道
新闻:3f ******** @ news1.prserv.net ...这两个之间有什么区别吗?
int i = f();
如果(i)
和
int i;
if(i = f( ))
如果我在定义中初始化了,似乎后者会保存作业?对于班级类型或其他类型,它的工作方式是否有所不同?
我不太了解第一个问题,但对于内置类型我
不认为你会发现任何性能差异。基本上,你是
询问之间的区别:
int i = 0;
和
int i;
i = 0;
对于类类型,您可能会注意到性能受到影响,因为
类是第一个默认构造然后分配给,而在
前片段中你只有一步,即构造。但这也是
取决于班级。
hth
-
jb
(如果你想通过电子邮件回复,用x替换y)
" Jakob Bieling <是ne ***** @ gmy.net>在消息中写道
news:bm ************* @ news.t-online.com ..." jeffc" <无**** @ nowhere.com>在消息中写道
新闻:3f ******** @ news1.prserv.net ...这两个之间有什么区别吗?
> int i = f();
如果(i)
int i;
if(i = f())
如果我在定义中初始化了
,似乎后者会保存赋值?对于类类型或
其他类型它会有不同的工作吗?
我不太了解第一个问题,但对于内置类型
我不认为你会注意到任何性能差异。基本上,你
询问的区别是:
int i = 0;
和
int i;
i = 0;
我的问题基本上是:它归结为什么?或者是否有一些
其他因素特别涉及if语句我不知道
会导致我的前两个例子被区别对待吗?
>这两个之间有什么区别吗?
int i = f();
if if(i)
int i;
if(i = f())
如果我在定义上初始化了,似乎后者会保存赋值?
否。在这两种情况下,首先评估f()然后结果将是'/ i'
对''i'的影响。
Jonathan
Is there any difference between these 2?
int i = f();
if ( i )
and
int i;
if ( i = f() )
It seems like the latter would save the assignment if i was initialized on
the definition anyway? Would it work differently for class types or other
types?
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...Is there any difference between these 2?
int i = f();
if ( i )
and
int i;
if ( i = f() )
It seems like the latter would save the assignment if i was initialized on
the definition anyway? Would it work differently for class types or other
types?
I did not quite understand the first question, but for built-in types I
do not think you will notice any performance difference. Basically, you are
asking about the difference between:
int i = 0;
and
int i;
i = 0;
For class types you may very well notice a performance hit, since the
class is first default constructed and then assigned to, whereas in the
former snippet you have only one step, the contruction. But this also
depends on the class.
hth
--
jb
(replace y with x if you want to reply by e-mail)
"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com..."jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...Is there any difference between these 2?
int i = f();
if ( i )
and
int i;
if ( i = f() )
It seems like the latter would save the assignment if i was initialized on the definition anyway? Would it work differently for class types or other types?
I did not quite understand the first question, but for built-in types
I do not think you will notice any performance difference. Basically, you are asking about the difference between:
int i = 0;
and
int i;
i = 0;
My question was basically: is that what it boils down to? Or is there some
other factor involved specifically with if statements I''m not aware of that
would cause my first 2 examples to be treated differently?
> Is there any difference between these 2?
int i = f();
if ( i )
and
int i;
if ( i = f() )
It seems like the latter would save the assignment if i was
initialized on the definition anyway?
No. In both cases, f() will be evaluated first and then the result will be
affected to ''i''.
Jonathan
这篇关于if表达的表现或其他差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!