# php自定义获取脚本执行时间函数

**URL:** <https://www.shiqidu.com/t/topic/219>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2017 年9 月 5 日 01:56 UTC](https://www.shiqidu.com/t/topic/219 "2017-09-05T01:56:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![caojie](https://www.shiqidu.com/user_avatar/www.shiqidu.com/caojie/32/465_2.png) [@caojie](https://www.shiqidu.com/u/caojie)\
发布日期： [2017 年9 月 5 日 01:56 UTC](https://www.shiqidu.com/t/topic/219/1 "2017-09-05T01:56:10Z")

</div>

```php
function get_execution_time()
{
    static $microtime_start = null;
    if($microtime_start === null)
    {
        $microtime_start = microtime(true);
        return 0.0;
    }
    return microtime(true) - $microtime_start;
}
// 生成一个包含10万个元素的数组并打乱
$arr1 = range(0, 99999);
shuffle($arr1);
get_execution_time();

// 执行排序
sort($arr1);

// 计算数组排序所需时间
echo "花费了 " . get_execution_time() . " s <br>";

# output
// 花费了 0.113119840622 s 

```
