如果负数不是一个因素,与零相比是否有可量化的好处?
Is there a quantifiable benefit for comparing to zero if negative numbers are not a factor?
假设负数不是一个因素;使用大于或等于运算符比较零是否有明确的、可量化的好处(例如性能差异)?
例如,比较下面的两个IF语句:
if (x > 0) {
functionOne();
} else {
functionTwo();
}
if (x == 0) {
functionTwo();
} else {
functionOne();
}
它们没有明显的不同。在 billions of runs of each version, the speed difference is negligible (link to jsPerf demo).
样本结果运行:
x operator ops/sec
1 == 1,899,677,783
0 == 1,896,694,559
1 > 1,886,826,964
0 > 1,893,169,247
这些根本不是有意义的差异。
假设负数不是一个因素;使用大于或等于运算符比较零是否有明确的、可量化的好处(例如性能差异)?
例如,比较下面的两个IF语句:
if (x > 0) {
functionOne();
} else {
functionTwo();
}
if (x == 0) {
functionTwo();
} else {
functionOne();
}
它们没有明显的不同。在 billions of runs of each version, the speed difference is negligible (link to jsPerf demo).
样本结果运行:
x operator ops/sec
1 == 1,899,677,783
0 == 1,896,694,559
1 > 1,886,826,964
0 > 1,893,169,247
这些根本不是有意义的差异。