search
Homephp教程php手册php microtime获取浮点的时间戳

php microtime获取浮点的时间戳

Jun 13, 2016 pm 12:18 PM
phpcodefunctioncopytimefloating pointuseofObtainthis

一直以来用这个函数获取:

复制代码 代码如下:


function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}


看到别人的源码中用microtime(true),查了下手册,原来从PHP 5.0.0 开始,microtime增加了这个参数。
引用

复制代码 代码如下:


mixed microtime ( [bool get_as_float] )
microtime() 当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
如果给出了 get_as_float 参数并且其值等价于 TRUE,microtime() 将返回一个浮点数。
注意: get_as_float 参数是 PHP 5.0.0 新加的。


如果程序肯定在PHP5以上的环境运行,那么就直接使用microtime(true)吧,比使用microtime_float函数至少快两倍。以下是我简单测试的程序代码。

复制代码 代码如下:


function microtime_float3(){
return microtime(true);
}
function microtime_float2(){
if( PHP_VERSION > 5){
return microtime(true);
}else{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
}
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function runtime($t1){
return number_format((microtime_float() - $t1)*1000, 4).'ms';
}
$t1 = microtime_float();
for($i=0;$imicrotime_float();
}
echo "microtime_float=====";
echo runtime($t1).'
';
$t1 = microtime(true);
for($i=0;$imicrotime(true);
}
echo "microtime_true=====";
echo runtime($t1).'
';
$t1 = microtime(true);
for($i=0;$imicrotime_float2();
}
echo "microtime_float2=====";
echo runtime($t1).'
';
$t1 = microtime(true);
for($i=0;$imicrotime_float3();
}
echo "microtime_float3=====";
echo runtime($t1).'
';
?>


本机winxp运行结果:
microtime_float=====109.5631ms
microtime_true=====38.8160ms
microtime_float2=====52.7902ms
microtime_float3=====45.0699ms
Linux上运行结果:
microtime_float=====47.2510ms
microtime_true=====9.2051ms
microtime_float2=====16.3319ms
microtime_float3=====12.2800ms
在PHP5的环境下,直接使用microtime(true);显然是最快的。microtime_float2和microtime_float3都可以在不改变原有程序的情况下,直接修改函数内容来实现性能的轻微提升。microtime_float2可以做为兼容旧版本的写法。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor