想象一下以下代码:
void DoThis()
{
if (!isValid) return;
DoThat();
}
void DoThat() {
Console.WriteLine("DoThat()");
}
Run Code Online (Sandbox Code Playgroud)
在void方法中使用return是否可以?它有任何性能损失吗?或者写一个这样的代码会更好:
void DoThis()
{
if (isValid)
{
DoThat();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一些数据分析代码,并且必须根据某些标准排除样本.在实践中,我最终编写如下代码:
bool Test(SampleType sample)
{
if( ! SubTest1(sample) )
return false;
if( ! SubTest2(sample) )
return false;
if( ! SubTest3(sample) )
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
以下似乎与我相同:
bool Test(SampleType sample)
{
if( ! SubTest1(sample) )
return false;
else if( ! SubTest2(sample) )
return false;
else if( ! SubTest3(sample) )
return false;
else
return true;
}
Run Code Online (Sandbox Code Playgroud)
计算成本方面有区别吗?在可扩展性/可维护性,美学等方面是否存在可论证的优先考虑?
我知道这可能是一个无关紧要的问题,但是一旦我把这些问题困在脑子里,我就需要找到答案.
PS:如果有人关心,我的实际代码截至15/09可以在以下网址找到:http: //folk.uio.no/henrikq/conf.tgz
我和一位同事对以下哪一项更优雅存在争议.我不会说谁是谁,所以它是公正的.哪个更优雅?
public function set hitZone(target:DisplayObject):void
{
if(_hitZone != target)
{
_hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);
_hitZone = target;
_hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);
}
}
Run Code Online (Sandbox Code Playgroud)
...要么...
public function set hitZone(target:DisplayObject):void
{
if(_hitZone == target)return;
_hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);
_hitZone = target;
_hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);
}
Run Code Online (Sandbox Code Playgroud) 最近我们找到了一个"好方法",通过使用continue来注释掉代码行:
for(int i=0; i<MAX_NUM; i++){
....
.... //--> about 30 lines of code
continue;
....//--> there is about 30 lines of code after continue
....
}
Run Code Online (Sandbox Code Playgroud)
我问问为什么前面的开发人员将continue关键字放在密集循环中.最有可能的是他/她认为放置"继续"关键字而不是删除所有不需要的代码更容易......
通过查看以下场景,它引发了另一个问题:
情景A:
for(int i=0; i<MAX_NUM; i++){
....
if(bFlag)
continue;
....//--> there is about 100 lines of code after continue
....
}
Run Code Online (Sandbox Code Playgroud)
情景B:
for(int i=0; i<MAX_NUM; i++){
....
if(!bFlag){
....//--> there is about 100 lines of code after continue
....
}
}
Run Code Online (Sandbox Code Playgroud)
你觉得哪个最好?为什么?break关键字怎么样?
最后几周,我发现自己在很多const地方使用了很多.不仅在方法或参数声明中,甚至在临时变量中.
让我用一个简单的函数来说明.
我以前写过:
// A dummy function that sums some computation results
unsigned int sum_results(const Operation& p1, const Operation& p2)
{
unsigned int result1 = p1.computeResult();
unsigned int result2 = p2.computeResult();
// Well this function could be in one single line but
// assume it does more complex operations
return result1 + result2;
}
Run Code Online (Sandbox Code Playgroud)
但现在更像是:
// A dummy function that sums some computation results
unsigned int sum_results(const Operation& p1, const Operation& p2)
{
const unsigned int result1 = …Run Code Online (Sandbox Code Playgroud) 我遇到一道leetcode题加油站
但是我发现如果我使用if/else而不是我的代码会稍微快一些if/continue。
编辑:在摆弄测试用例之后。我发现问题是continue
clang 似乎正在continue认真对待..?我的猜测是 hat clang 以某种方式尝试将继续和循环检查之前的部分放在一起。
简而言之:我想知道:
编辑旁注:删除分支预测标签。
我应该使用 return/continue 语句而不是 if-else 吗?看起来它们应该几乎没有区别。
编辑:演示显示问题是continue
我用10k的数据集测试代码,发现问题出在continue本身。
我把 放在continuebodyif的末尾,然后运行时间增加了30% ~ 50%,这仍然让我感到惊讶。
现场演示
数量差异:11070 至 15060(带continue)
编辑于 2022/5/30:抱歉,我发现我之前的天真分析错误地优化了部分计算...这导致了 100 的差异... 糟糕的演示
#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>
using namespace std; // Yeah I know it's …Run Code Online (Sandbox Code Playgroud)