否则包退? [英] else or return?

查看:92
本文介绍了否则包退?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个出以下两个是性能和标准的做法最好WRT。内部如何处理.NET这两个代码段?



代码1

 如果(结果)
{
过程1();
}
,否则
{
过程2();
}

或代码2



 如果(结果)
{
过程1();
的回报;
}
过程2();


解决方案

性能差异,如果有的话,在任何可以忽略不计正常的情况下。



(其中包括)一个标准的做法是尽量保持一个单一的退出点从一个方法,这样有利于第一选择的会谈。



在实际执行的收益在中间是最容易使跳转到的方法,其中的结束代码的收官之栈帧的方法,所以很可能是最后的可执行代码是两个代码相同。


Which one out of following two is best wrt to performance and standard practice. How does .NET internally handles these two code snippets?

Code1

If(result)
{
  process1();
}
else
{
  process2();
}

Or Code 2

If(result)
{
   process1();
   return;
}
process2();

解决方案

The performance difference, if any, is negligible in any normal case.

One standard practice (among others) is to try to keep a single exit point from a method, so that talks in favour of the first alternative.

The actual implementation for the return in the middle is most likely to make a jump to the end of the method, where the code to wrap up the stack frame for the method is, so it's likely that the final executable code is identical for the two codes.

这篇关于否则包退?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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