嵌套ifs和速度。 [英] Nested ifs and speed.

查看:114
本文介绍了嵌套ifs和速度。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到过s函数(实际上很多),它的结构是一个嵌套的ifs系列嵌套ifs,其功能位于正中心:


if(condition1)

if(condition2)

if(condition3)

if(condition4)

< Body>

else {

printf(" Condition4 failed");

return;

}

else {

printf(" Condition3 failed");

return;

}

else {

printf(" Condition2 failed");

return;

}

否则{

printf(条件1失败);

返回;

}


对我来说这真是难以理解,我会重写为:


if(!condition1){

printf(" Condition 1 failed?);

返回;

}


....

< Body>

我想到你为什么要用这种方式编写代码;分离

条件的后果。我发现可能有关于跳跃的表现相关问题,所以使用

原始代码,在所有条件超过的情况下,你得不到

跳转但是使用我的代码你可以获得与

条件一样多的跳转,因为每个if块都需要被跳过。我是

肯定不是编译器或机器代码的专家,这是

可能是一个过早优化的情况,无论如何我只是尝试

进入首先写下它的人的头脑。


干杯,

查理。

解决方案

" charlie" < ch ************* @ gmail.comwrote:


我最近遇到了s函数(实际上很多),其结构是a

系列嵌套ifs,功能位于正中心:


if(condition1)

if(条件2)

if(condition3)

if(condition4)

< Body>

else {

printf(" Condition4 failed");

return;

}

else {

printf(" Condition3 failed");

return;

}

else {

printf( 条件2失败);

返回;

}

else {

printf(" Condition1 failed" ;);

返回;

}


对我来说这真是难以理解,我会重写为:


if(!condition1){

printf(条件1失败);

返回;

}

...

< Body>


我想到你为什么要用这种方式编写代码;分离

条件的后果。它发生在我身上可能有关于跳跃的表现相关问题



Nah。它可能只是另一个带有闪光灯的Wirthian:一次入场,

一次退出,或者其他!


Richard




Richard Bos写道:


>

Nah。它可能只是另一个带有闪光灯的Wirthian:一个条目,

一个退出,或者其他!


Richard



实际上,我只是再次查看代码并看到其他

分支不包含return语句所以你可能是正确的:

if(condition1)

if(condition2)

if(condition3)

if(condition4)

< Body>

else {

printf(" Condition4 failed");

}

else {

printf(" Condition3 failed");

}

else {

printf(" ;条件2失败);

}

else {

printf(条件1失败);

}


charlie写道:


>

Richard Bos写道:


>>
Nah。它可能只是另一个带有闪光灯的Wirthian:一个入口,一个出口,或者其他!

理查德



实际上,我只是再次查看代码并看到其他

分支不包含return语句所以你可能是正确的:


if(condition1 )

if(condition2)

if(condition3)

if(condition4)

< Body>

else {

printf(" Condition4 failed");

}

else {

printf(条件3失败);

}

else {

printf(" Condition2 failed");

}

else {

printf(" Condition1 failed");

}



但是,如果(!c1)printf(" blah");
$ b,该代码可以写成更紧密的关联


$ b else if(!c2)printf(" bleh" );

else if(!c3)printf(" blih");

else if(!c4)printf(" bloh");

else< body>


(布局和大括号的味道),所以原来的布局没有

建议不得不坚持一个出口的风格。


[所有这些printfs的末尾应该有一个\ n吗?]


-

Chris" seeker" Dollin

我还在这里,我正在拿着答案 - 卡纳塔克邦,/爱与感情/


I recently came across s function (actually many) whose structure is a
series of nested ifs with the meat of the function at the very centre:

if (condition1)
if (condition2)
if (condition3)
if (condition4)
<Body>
else {
printf("Condition4 failed");
return;
}
else {
printf("Condition3 failed");
return;
}
else {
printf("Condition2 failed");
return;
}
else {
printf("Condition1 failed");
return;
}

To me this is really unreadable and I would re-write as:

if(!condition1) {
printf("Condition 1 failed");
return;
}

....
<Body>
I got to thinking why would you write code this way; seperating the
condition from it''s consequences. It occured to me that there may have
been performance related concerns with respect to jumps so with the
original code, in the case of all conditions suceeding, you get no
jumps but with my code you may get as many jumps as there are
conditions because each if block would need to be skipped. I am
certainly not an expert in compilers or machine code and this is
probably a case of premature optimization in either case I just trying
to get into the head of the guy that wrote it in the first place.

Cheers,
Charlie.

解决方案

"charlie" <ch*************@gmail.comwrote:

I recently came across s function (actually many) whose structure is a
series of nested ifs with the meat of the function at the very centre:

if (condition1)
if (condition2)
if (condition3)
if (condition4)
<Body>
else {
printf("Condition4 failed");
return;
}
else {
printf("Condition3 failed");
return;
}
else {
printf("Condition2 failed");
return;
}
else {
printf("Condition1 failed");
return;
}

To me this is really unreadable and I would re-write as:

if(!condition1) {
printf("Condition 1 failed");
return;
}

...
<Body>

I got to thinking why would you write code this way; seperating the
condition from it''s consequences. It occured to me that there may have
been performance related concerns with respect to jumps

Nah. It''s probably just another Wirthian with blinkers on: One Entry,
One Exit, Or Else!

Richard



Richard Bos wrote:

>
Nah. It''s probably just another Wirthian with blinkers on: One Entry,
One Exit, Or Else!

Richard

Actually, I just looked at the code again and saw that the else
branches do not contain return statement so you may well be correct:

if (condition1)
if (condition2)
if (condition3)
if (condition4)
<Body>
else {
printf("Condition4 failed");
}
else {
printf("Condition3 failed");
}
else {
printf("Condition2 failed");
}
else {
printf("Condition1 failed");
}


charlie wrote:

>
Richard Bos wrote:

>>
Nah. It''s probably just another Wirthian with blinkers on: One Entry,
One Exit, Or Else!

Richard


Actually, I just looked at the code again and saw that the else
branches do not contain return statement so you may well be correct:

if (condition1)
if (condition2)
if (condition3)
if (condition4)
<Body>
else {
printf("Condition4 failed");
}
else {
printf("Condition3 failed");
}
else {
printf("Condition2 failed");
}
else {
printf("Condition1 failed");
}

But that code could be written with closer association as

if (!c1) printf( "blah");
else if (!c2) printf( "bleh" );
else if (!c3) printf( "blih" );
else if (!c4) printf( "bloh" );
else <body>

(layout and braces to taste), so the original layout doesn''t
suggest having to stick to the one-exit style.

[Should there be a \n at the end of all these printfs?]

--
Chris "seeker" Dollin
"I''m still here and I''m holding the answers" - Karnataka, /Love and Affection/


这篇关于嵌套ifs和速度。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆