1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-25 23:52:17 +01:00
Files
2025-11-06 09:04:46 +08:00

127 lines
3.6 KiB
XML
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 3a8c3e77df070a046c9d5b56b68926ca2d7e5ee3 Maintainer: mowangjuanzi Status: ready -->
<refentry xml:id="function.microtime" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>microtime</refname>
<refpurpose>返回当前 Unix 时间戳和微秒数</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>string</type><type>float</type></type><methodname>microtime</methodname>
<methodparam choice="opt"><type>bool</type><parameter>as_float</parameter><initializer>&false;</initializer></methodparam>
</methodsynopsis>
<para>
<function>microtime</function> 返回当前 Unix
时间戳以及微秒数。本函数仅在支持 gettimeofday()
系统调用的操作系统下可用。
</para>
<para>
对于性能测量,建议使用 <function>hrtime</function>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>as_float</parameter></term>
<listitem>
<para>
如果使用并设置为 &true;<function>microtime</function>
将返回浮点数而不是字符串,具体查看下面的返回值相关描述。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
默认情况下,<function>microtime</function> 返回“msec sec”形式的 <type>string</type>,其中
<literal>sec</literal> 是自 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日
0:00:00以来的秒数<literal>msec</literal> 是自 <literal>sec</literal>
以来经过的微秒,表示为秒数的小数部分。
</para>
<para>
如果 <parameter>as_float</parameter> 设置为 &true;,然后 <function>microtime</function>
返回 <type>float</type>,表示自 Unix 纪元以来的当前时间,以秒为单位,精确到最接近的微秒。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>计时脚本执行时间</title>
<programlisting role="php">
<![CDATA[
<?php
$time_start = microtime(true);
// Sleep 一会
usleep(10_000);
$time_end = microtime(true);
$time = $time_end - $time_start;
print "Did nothing in $time seconds\n";
?>
]]>
</programlisting>
</example>
<example>
<title><function>microtime</function><literal>REQUEST_TIME_FLOAT</literal></title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
// 随机 sleep 时间
usleep(random_int(10_000, 1_000_000));
// 在 $_SERVER 超全局数组中 REQUEST_TIME_FLOAT 是有效的。
// 包含请求开始的时间戳,精度为微秒。
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>time</function></member>
<member><function>hrtime</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->