microtime() 函数返回当前 Unix 时间戳的微秒数。这个函数在需要精确计时、性能测量和基准测试时非常有用,可以获取到微秒级的精度。
microtime([bool $get_as_float = false]) : string|float
| 参数 | 描述 |
|---|---|
| get_as_float |
可选。 如果设置为 如果设置为 默认值为 |
| get_as_float 值 | 返回值 |
|---|---|
false(默认) |
返回一个字符串,格式为 "微秒 秒" 例如: 其中 "0.123456" 是微秒部分,"1679876543" 是秒数 |
true |
返回一个浮点数,表示自 Unix 纪元(1970-01-01 00:00:00.000000)以来的秒数 例如: 这个值适合进行算术运算 |
<?php
$microtime = microtime();
echo $microtime;
// 输出类似:0.123456 1679876543
// 第一部分是微秒,第二部分是秒
// 分割字符串
list($microseconds, $seconds) = explode(' ', $microtime);
echo "微秒:" . $microseconds . "<br>";
echo "秒数:" . $seconds;
?>
<?php
$microtime = microtime(true);
echo $microtime;
// 输出类似:1679876543.123456
// 可以用于精确的时间计算
echo round($microtime, 6); // 四舍五入到6位小数
?>
<?php
// 开始时间
$start = microtime(true);
// 模拟一些耗时操作
for ($i = 0; $i < 1000000; $i++) {
// 空循环
}
// 结束时间
$end = microtime(true);
// 计算执行时间
$execution_time = $end - $start;
echo "脚本执行时间:" . round($execution_time, 6) . " 秒";
echo "<br>";
echo "脚本执行时间:" . ($execution_time * 1000) . " 毫秒";
// 输出类似:
// 脚本执行时间:0.023456 秒
// 脚本执行时间:23.456 毫秒
?>
<?php
// 测试算法1的性能
$start1 = microtime(true);
$result1 = 0;
for ($i = 1; $i <= 1000000; $i++) {
$result1 += $i;
}
$end1 = microtime(true);
$time1 = $end1 - $start1;
// 测试算法2的性能
$start2 = microtime(true);
$result2 = 0;
$result2 = (1 + 1000000) * 1000000 / 2; // 等差数列求和公式
$end2 = microtime(true);
$time2 = $end2 - $start2;
echo "算法1(循环求和)结果:{$result1},耗时:" . round($time1, 6) . " 秒<br>";
echo "算法2(公式计算)结果:{$result2},耗时:" . round($time2, 6) . " 秒<br>";
echo "算法2比算法1快 " . round($time1 / $time2, 2) . " 倍";
// 输出类似:
// 算法1(循环求和)结果:500000500000,耗时:0.023456 秒
// 算法2(公式计算)结果:500000500000,耗时:0.000001 秒
// 算法2比算法1快 23456.00 倍
?>
<?php
function generateUniqueId() {
$microtime = microtime();
list($microseconds, $seconds) = explode(' ', $microtime);
// 移除小数点,只取数字
$microseconds = str_replace('0.', '', $microseconds);
// 返回微秒级的时间戳
return $seconds . $microseconds;
}
$uniqueId = generateUniqueId();
echo "生成的唯一ID:" . $uniqueId;
// 输出类似:1679876543123456
?>
<?php
// 获取毫秒级时间戳
function getMillisecond() {
list($microseconds, $seconds) = explode(' ', microtime());
$milliseconds = round($microseconds * 1000);
return $seconds * 1000 + $milliseconds;
}
$ms = getMillisecond();
echo "毫秒级时间戳:" . $ms;
// 输出类似:1679876543123
?>
<?php
// 获取当前时间的完整信息
function getDetailedTime() {
$microtime = microtime(true);
$seconds = floor($microtime);
$microseconds = round(($microtime - $seconds) * 1000000);
$date = date('Y-m-d H:i:s', $seconds);
return [
'timestamp' => $seconds,
'microseconds' => $microseconds,
'full_microtime' => $microtime,
'formatted_date' => $date,
'formatted_with_micro' => $date . '.' . str_pad($microseconds, 6, '0', STR_PAD_LEFT)
];
}
$timeInfo = getDetailedTime();
echo "完整时间信息:<pre>";
print_r($timeInfo);
echo "</pre>";
// 输出类似:
// 完整时间信息:
// Array
// (
// [timestamp] => 1679876543
// [microseconds] => 123456
// [full_microtime] => 1679876543.123456
// [formatted_date] => 2023-03-26 10:15:43
// [formatted_with_micro] => 2023-03-26 10:15:43.123456
// )
?>
测量代码段、函数或算法的执行时间,优化性能。
比较不同实现方式的效率,选择最佳方案。
生成基于时间戳的唯一标识符,避免冲突。
需要毫秒或微秒级精度的计时场景。
microtime(true) 返回浮点数,更适合进行时间计算microtime() 的精度可能较低(通常到毫秒级)microtime() 可能会影响性能