我有以下代码:
<?php
$start = 1;
$timestart = microtime(1);
for ($i = 0; $i < 1000000; $i++) {
$result1 = $start * 4;
}
echo "\n";
echo microtime(1) - $timestart;
echo "\n";
$timestart = microtime(1);
for ($i = 0; $i < 1000000; $i++) {
$result2 = $start << 2;
}
echo "\n";
echo microtime(1) - $timestart;
echo "\n";
这将输出:
0.14027094841003
0.12061500549316
我在网上找到了一道谷歌面试题(我想作为开发人员申请,但我意识到我无法通过),其中的一个问题是如何最快地乘一个数字。我的第一个想法是使用*
符号,所以我进行了测试。
我的问题是,为什么移位比乘法快?