大家好!为了缩短运算时间,我通过Stopwatch类记录了几个关键步骤的运行时间,代码是这样的:
首先,以下3个变量是本次提问会用到的:
//定义的部分
Dictionary<string, node> AllNodes = new Dictionary<string, node>();
Dictionary<string, node> CloseList = new Dictionary<string, node>();
string nextCheckingId;
然后,以下是代码V1
while(条件省略)
{
//省略无关代码
watch1.Start();//watch1是1个Stopwatch的对象,下同
if (!AllNodes.ContainsKey(nextCheckingId) || CloseList.ContainsKey(nextCheckingId))
{
continue;
}
watch1.Stop();
//省略无关代码
}
//运行后输出watch1,经多次测算:
// watch1=73ms
为了缩短这一段的时延,想看看哪一句耗时多,于是改成了以下代码V2
while(条件省略)
{
//省略无关代码
watch1.Start();
watch1_1.Start();
bool b1 = AllNodes.ContainsKey(nextCheckingId);
watch1_1.Stop();
watch1_2.Start();
bool b2 = CloseList.ContainsKey(nextCheckingId);
watch1_2.Stop();
watch1_3.Start();
if (!b1 || b2) continue;
watch1_3.Stop();
watch1.Stop();
//省略无关代码
}
//运行后输出以上几个watch,经多次测算:
// watch1=80ms
// watch1_1=5ms
// watch1_2=4ms
// watch1_3=71ms
我想请问,为何if(!b1 || b2)这句会占用这么多的时间?是否我的测算方法有误?怎样才能尽量缩短这一段的运行时长呢?感谢~
(C币余额不足,无法发布悬赏了,抱歉)