PHP statistical time code sharing

小云云
Release: 2023-03-20 14:48:02
Original
1776 people have browsed it

This article mainly shares with you code examples of PHP statistics time and memory usage, hoping to help everyone.

<?php
/**
 * 记录和统计时间(微秒)和内存使用情况
 * 使用方法:
 * <code>
 * G(&#39;begin&#39;); // 记录开始标记位
 * // ... 区间运行代码
 * G(&#39;end&#39;); // 记录结束标签位
 * echo G(&#39;begin&#39;,&#39;end&#39;,6); // 统计区间运行时间 精确到小数后6位
 * echo G(&#39;begin&#39;,&#39;end&#39;,&#39;m&#39;); // 统计区间内存使用情况
 * 如果end标记位没有定义,则会自动以当前作为标记位
 * 其中统计内存使用需要 MEMORY_LIMIT_ON 常量为true才有效
 * </code>
 * @param string $start 开始标签
 * @param string $end 结束标签
 * @param integer|string $dec 小数位或者m 
 * @return mixed
 */
function G($start,$end=&#39;&#39;,$dec=4) {
    static $_info       =   array();
    static $_mem        =   array();
    if(is_float($end)) { // 记录时间
        $_info[$start]  =   $end;
    }elseif(!empty($end)){ // 统计时间和内存使用
        if(!isset($_info[$end])) $_info[$end]       =  microtime(TRUE);
        if(MEMORY_LIMIT_ON && $dec==&#39;m&#39;){
            if(!isset($_mem[$end])) $_mem[$end]     =  memory_get_usage();
            return number_format(($_mem[$end]-$_mem[$start])/1024);          
        }else{
            return number_format(($_info[$end]-$_info[$start]),$dec);
        }      
    }else{ // 记录时间和内存使用
        $_info[$start]  =  microtime(TRUE);
        if(MEMORY_LIMIT_ON) $_mem[$start]           =  memory_get_usage();
    }
}
Copy after login

Related recommendations:

php statistical time and memory usage example sharing_PHP tutorial

How to get memory usage in PHP

How to implement php database statistical timestamp grouping and outputting data by day

The above is the detailed content of PHP statistical time code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!