Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 154.0MB ·虚拟内存 1439.6MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
如果要测量执行一组行所花费的时间或内存使用情况,可以使用 CodeIgniter 中的 Benchmarking 点来计算。 CodeIgniter 中有一个单独的" 基准测试"类用于此目的。
这个类是自动加载的;你不必加载它。它可以在您的控制器、视图和模型类中的任何地方使用。您所要做的就是标记起点和终点,然后在这两个标记点之间执行 elapsed_time() 函数,您就可以得到执行该代码所需的时间,如下所示。
<?php
$this->benchmark->mark('code_start');
// Some code happens here
$this->benchmark->mark('code_end');
echo $this->benchmark->elapsed_time('code_start', 'code_end');
?>
要显示内存使用情况,请使用函数 memory_usage(),如下面的代码所示。
<?php echo $this->benchmark->memory_usage(); ?>
创建一个名为 Profiler_controller.php 的控制器并将其保存在 application/controller/Profiler_controller.php
<?php
class Profiler_controller extends CI_Controller {
public function index() {
//enable profiler
$this->output->enable_profiler(TRUE);
$this->load->view('test');
}
public function disable() {
//disable profiler
$this->output->enable_profiler(FALSE);
$this->load->view('test');
}
}
?>
创建一个名为 test.php 的视图文件并将其保存在 application/views/test.php
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter View Example</title>
</head>
<body>
CodeIgniter View Example
</body>
</html>
更改 application/config/routes.php 中的 routes.php 文件,为上述控制器添加路由,并在文件末尾添加以下行。
$route['profiler'] = "Profiler_controller"; $route['profiler/disable'] = "Profiler_controller/disable"
之后,您可以在浏览器的地址栏中输入以下 URL 来执行示例。
http://yoursite.com/index.php/profiler
上面的 URL 将启用分析器,它会产生一个输出,如下面的屏幕截图所示。

要禁用分析,请执行以下 URL。
http://yoursite.com/index.php/profiler/disable
在 CodeIgniter 中添加 JavaScript 和 CSS(层叠样式表)文件非常简单。需要在根目录下创建JS和CSS文件夹,复制JS文件夹下的所有.js文件和CSS文件夹下的. ...