PHP microtime()函数

microtime() 函数返回当前 Unix 时间戳的微秒数。这个函数在需要精确计时、性能测量和基准测试时非常有用,可以获取到微秒级的精度。

语法

microtime([bool $get_as_float = false]) : string|float

参数说明

参数 描述
get_as_float

可选。 如果设置为 truemicrotime() 将返回一个浮点数(秒数)。

如果设置为 false 或省略,将返回一个字符串,格式为 "微秒 秒"。

默认值为 false

返回值

get_as_float 值 返回值
false(默认)

返回一个字符串,格式为 "微秒 秒"

例如:"0.123456 1679876543"

其中 "0.123456" 是微秒部分,"1679876543" 是秒数

true

返回一个浮点数,表示自 Unix 纪元(1970-01-01 00:00:00.000000)以来的秒数

例如:1679876543.123456

这个值适合进行算术运算

示例

示例 1:返回字符串格式(默认)

<?php
$microtime = microtime();
echo $microtime;
// 输出类似:0.123456 1679876543
// 第一部分是微秒,第二部分是秒

// 分割字符串
list($microseconds, $seconds) = explode(' ', $microtime);
echo "微秒:" . $microseconds . "<br>";
echo "秒数:" . $seconds;
?>

示例 2:返回浮点数格式

<?php
$microtime = microtime(true);
echo $microtime;
// 输出类似:1679876543.123456

// 可以用于精确的时间计算
echo round($microtime, 6); // 四舍五入到6位小数
?>

示例 3:测量脚本执行时间

<?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 毫秒
?>

示例 4:比较不同算法的性能

<?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 倍
?>

示例 5:生成唯一ID(基于时间戳)

<?php
function generateUniqueId() {
    $microtime = microtime();
    list($microseconds, $seconds) = explode(' ', $microtime);

    // 移除小数点,只取数字
    $microseconds = str_replace('0.', '', $microseconds);

    // 返回微秒级的时间戳
    return $seconds . $microseconds;
}

$uniqueId = generateUniqueId();
echo "生成的唯一ID:" . $uniqueId;
// 输出类似:1679876543123456
?>

示例 6:获取毫秒级的时间戳

<?php
// 获取毫秒级时间戳
function getMillisecond() {
    list($microseconds, $seconds) = explode(' ', microtime());
    $milliseconds = round($microseconds * 1000);
    return $seconds * 1000 + $milliseconds;
}

$ms = getMillisecond();
echo "毫秒级时间戳:" . $ms;
// 输出类似:1679876543123
?>

示例 7:格式化微秒时间

<?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
// )
?>

常见应用场景

性能测试

测量代码段、函数或算法的执行时间,优化性能。

基准测试

比较不同实现方式的效率,选择最佳方案。

唯一ID生成

生成基于时间戳的唯一标识符,避免冲突。

高精度计时

需要毫秒或微秒级精度的计时场景。

注意事项

  • microtime(true) 返回浮点数,更适合进行时间计算
  • 在 Windows 系统上,microtime() 的精度可能较低(通常到毫秒级)
  • 对于性能测量,建议多次运行代码取平均值,以减少系统负载波动的影响
  • 在生产环境中频繁调用 microtime() 可能会影响性能
  • 浮点数计算可能存在精度问题,需要注意比较和舍入

相关函数

  • time() - 返回当前时间的 Unix 时间戳(秒)
  • hrtime() - 获取高精度时间(纳秒级)
  • date() - 格式化本地时间/日期