A-A+
给wordpress添加一个页面执行效率的查询
我们在不少的站点都能看到这样的一个提示“该页面执行查询0.5秒”等等这样的字眼,凡是页面有执行查询的站点速度往往不会很慢,有这样的一个提示也能让我们时刻了解自己站点的访问速度如何,在 wordpress 搭建的站点中,前期可以使用动态的页面,但随着访问量的增多,每一次的查询都要连接服务器进行查询 mysql 服务器,这样的每一次查询都会消耗一次服务器资源,下面夏日博客先提供一段在一个页面输出 wordpress 执行效率时间的代码:
<?php class runtime { var $StartTime = 0; var $StopTime = 0; function get_microtime() { list($usec, $sec) = explode(' ', microtime()); return ((float)$usec + (float)$sec); } function start() { $this->StartTime = $this->get_microtime(); } function stop() { $this->StopTime = $this->get_microtime(); } function spent() { return round(($this->StopTime - $this->StartTime) * 1000, 1); } } $runtime= new runtime; $runtime->start(); $a = 0; for($i=0; $i<1000000; $i++) { $a += $i; } $runtime->stop(); echo "页面执行时间: ".$runtime->spent()." 毫秒"; ?>
将本代码放入到你的博客主题页面中即可,一般都是放到底部 footer.php 页脚中,这样在运行的时候就显示了页面执行时间 xx 毫秒,夏日博客给出的建议是尽量将自己的页面生成纯静态 html 页面,这样就不用频繁的去连接数据库了,而对于数据库我们也要安装一款数据库的优化插件,尽可能的少去连接 mysql 以此来进行博客的优化。