PHP - 计算执行程序耗时

效果:

 

首先在includes文件夹下编写,global.func.php函数库:

<?php

    /*
    *     Version:1.0
    *    CreateTime:2015年11月11日
    *    Author:HF_Ultrastrong
    */
    
    /**
     *_runtirm():使用来生成程序耗时
     *@access public:公开访问
     *@return float :返回值为浮点型
     *
     * */
    function _runtime(){
        $mtime = microtime();
        $time = explode(' ', $mtime);
        return $time[1]+$time[0];
    }

?>

 

然后在common.inc.php公共文件中引用:

<?php

    /*
    *     Version:1.0
    *    CreateTime:2015年11月11日
    *    Author:HF_Ultrastrong
    */
    
    //判断页面是否为非法调用
    if (!defined('IN_TG')) {
        //不是正常调用,跳转到404页面
        header('Location:'.'../error/404.html');
    }
    
    //定义相对于项目的绝对路径,用以加快调用页面的速度。
    define('ROOT_PATH', substr(dirname(__FILE__),0,-8));
    
    //判断PHP版本是否太低
    if (PHP_VERSION < '4.1.0') {
        echo 'Version is to Low!';
        exit;
    }
    
    //引入函数库global.func.php
    require ROOT_PATH.'/includes/global.func.php';
?>

 

然后将common公共文件在index.php主文件中引用:

 

之后在header.inc.php中生成开始时间:

  • 将生成时间存入常量

 

 

之后再footer.inc.php文件写下,结束时间,然后相减,得到耗时。

<?php

    /*
    *     Version:1.0
    *    CreateTime:2015年11月11日
    *    Author:HF_Ultrastrong
    */
    //判断页面是否为非法调用
    if (!defined('IN_TG')) {
        //不是正常调用,跳转到404页面
        header('Location:'.'../error/404.html');
    }
?>
<div id="footer">
    <p>本程序执行耗时:<?php echo '[ '.round(_runtime()- START_TIME,5).'s ]'; ?></p>
    <p>版权所有,违者必究 --- HF_Ultrastrong@copyright</p>
</div>

 

转载于:https://www.cnblogs.com/KTblog/p/4958098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在方法前后添加时间戳,然后计算时间差来计算方法耗时。也可以使用Spring AOP的拦截器,在方法执行前后记录时间戳,然后计算时间差。以下是一个使用Spring AOP计算方法耗时的示例: 1. 定义一个切面类,实现MethodInterceptor接口 ```java @Component @Aspect public class TimingAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long startTime = System.currentTimeMillis(); Object result = invocation.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(invocation.getMethod().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 2. 在配置文件中启用AOP,并将切面类作为切点 ```xml <aop:aspectj-autoproxy /> <bean id="timingAspect" class="com.example.TimingAspect" /> <aop:config> <aop:aspect ref="timingAspect"> <aop:pointcut expression="execution(* com.example.service.*.*(..))" /> <aop:around method="invoke" /> </aop:aspect> </aop:config> ``` 3. 在需要计算耗时的方法上加上@LogExecutionTime注解 ```java @Service public class MyService { @LogExecutionTime public void doSomething() { // do something } } ``` 4. 定义@LogExecutionTime注解和切面类 ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogExecutionTime { } @Component @Aspect public class TimingAspect { @Around("@annotation(com.example.LogExecutionTime)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println(joinPoint.getSignature().getName() + " 方法耗时:" + elapsedTime + "ms"); return result; } } ``` 这样,在调用doSomething方法时,就会输出方法耗时的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值