我只是想查看使用||
(双管,短路操作员,应该更快的(而不是|
。
,但结果没有深入了解实际的性能改进。如果我们只是先重新排序以下代码..首先使用|
执行控制台登录,那么它将显示的时间比另一个时间更多。
-
如何使用这种方式或任何其他方式看到性能差异?另外,
-
有人可以解释为什么我们更改顺序时会有区别?
如果
performance.now();
的行为是这样,我看不到使用它。
请帮忙。谢谢:(
var t3 = performance.now();
console.log(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity))
var t4 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
var t0 = performance.now();
console.log(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity))
var t1 = performance.now();
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
- 我如何使用这种方式或任何其他方式看到性能差异?
- 有人可以解释为什么我们更改订单时会有区别?
- 做很多测试。单个测试(尤其是花费很少时间的测试(具有浏览器或操作系统干扰的极端风险,这些风险在任何时候都可以决定暂停执行,做其他事情,然后再回来。
- 热身环境。几乎所有执行输入/输出(例如
console.log
(的所有内容都需要初始化,这将在第一次使用时进行,这可以轻松地破坏测得的性能。 - 剥离所有不严格属于您的测试的所有内容。如果要比较
|
与||
,则不要在定时的块中包括console.log
。
一些演示:
运行以下代码几次。观察:
- 通常(但并非总是(第一个需要更长的时间。那是初始化。
- 有时需要多次多次。那是干扰。
var t3 = performance.now();
console.log("herp");
var t4 = performance.now();
var t0 = performance.now();
console.log("derp");
var t1 = performance.now();
console.log("Call with herp took " + (t4 - t3) + " milliseconds.");
console.log("Call with derp took " + (t1 - t0) + " milliseconds.");
还观察到I/O对性能有可怕的影响:(您需要向下滚动(
var discard = function(){}; // just discards everything
var t3 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard("");
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 1000; ++i)
{
console.log("");
}
var t1 = performance.now();
console.log("discard() took " + (t4 - t3) + " milliseconds.");
console.log("console.log() took " + (t1 - t0) + " milliseconds.");
考虑到所有这些,让我们编写一个测试案例来测试您的操作:
var discard = function(){};
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity));
}
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity));
}
var t3 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (console.log(3)),
1 || (Infinity));
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 1000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (console.log(3)),
1 | (Infinity));
}
var t1 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
请注意,几乎所有时间都需要|
的情况才能完成console.log
,这仅证明||
上的短路确实有效。现在,对于|
与||
操作,这当然是准确的,该操作实际上涉及I/O。但是,如果您将其遗漏,结果再次很近:
(请注意,我将for循环提高到1000万,仅证明实际上花了多少时间 console.log
!(
var discard = function(){};
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (Infinity));
}
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (Infinity));
}
var t3 = performance.now();
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 || (0),
1 || (-1),
1 || ("a string"),
1 || (""),
1 || (null),
1 || (undefined),
1 || (Infinity));
}
var t4 = performance.now();
var t0 = performance.now();
for(var i = 0; i < 10000000; ++i)
{
discard(1 | (1),
1 | (0),
1 | (-1),
1 | ("a string"),
1 | (""),
1 | (null),
1 | (undefined),
1 | (Infinity));
}
var t1 = performance.now();
console.log("Call with (double) '||' took " + (t4 - t3) + " milliseconds.");
console.log("Call with single | took " + (t1 - t0) + " milliseconds.");
尽管所有这些,但|
和||
做完全不同的事情。不仅在执行方面,而且在结果方面。尝试1 || 126
vs 1 | 126
,或"a" || "b"
vs "a" | "b"
。