测试结果:这点差距建议忽略不计,还是用 % 比较通俗易懂
测试代码
$run_times = 100000000; // 读作 壹亿
$times = $run_times;
$start_time = microtime(true);
while (--$times) {
0 % 2;
1 % 2;
2 % 2;
1000 % 2;
1001 % 2;
100000000 % 2;
100000001 % 2;
}
$end_time = microtime(true);
$elapsed = $end_time - $start_time;
echo "% cost {$elapsed} seconds" . PHP_EOL;
$times = $run_times;
$start_time = microtime(true);
while (--$times) {
0 & 1;
1 & 1;
2 & 1;
1000 & 1;
1001 & 1;
100000000 & 1;
100000001 & 1;
}
$end_time = microtime(true);
$elapsed = $end_time - $start_time;
echo "& cost {$elapsed} seconds" . PHP_EOL;
运行5次的结果
# 1
% cost 0.92668008804321 seconds
& cost 0.91673493385315 seconds
# 2
% cost 0.94068813323975 seconds
& cost 0.91913104057312 seconds
# 3
% cost 0.93546605110168 seconds
& cost 0.91442203521729 seconds
# 4
% cost 0.91190505027771 seconds
& cost 0.90930008888245 seconds
# 5
% cost 0.93385791778564 seconds
& cost 0.9063708782196 seconds