Home > php教程 > php手册 > 纯PHP Codeigniter(CI) ThinkPHP效率测试

纯PHP Codeigniter(CI) ThinkPHP效率测试

WBOY
Release: 2016-06-06 19:49:09
Original
1252 people have browsed it

最近一直想做一个技术类的新闻站点,想做的执行效率高些,想用PHP做,一直纠结于用纯PHP做还是用CI或者THINKPHP。用纯PHP效率高,缺点n多,比如安全方面、构架方面等等等等;用CI、thinkPHP的话这些需要考虑的就少些,但是怕效率方面差得太多。后来就想先测测

最近一直想做一个技术类的新闻站点,想做的执行效率高些,想用PHP做,一直纠结于用纯PHP做还是用CI或者THINKPHP。用纯PHP效率高,缺点n多,比如安全方面、构架方面等等等等;用CI、thinkPHP的话这些需要考虑的就少些,但是怕效率方面差得太多。后来就想先测测吧,如果差得太多,就自己写个“框架”,满足自己的需求即可的框架。


CI版本是2.1.3,thinkphp用的是3.1。
因为大多数站点所做的事情就是查询数据库,因此此次的测试着重于数据库查询并显示。测试的数据库是dede的sys_enum,631条数据。目标就是查询出这些数据并显示到页面上计算消耗时间。

===========================================================================
1. 对于纯PHP,思路是在开始时记一下毫秒数,结束时记一下毫秒数,相减后得结果。纯PHP简单,直接贴代码:


01 02 $begin=microtime();
03 $begin=microtime_float($begin);
04 ?>
05
06
07 08 function microtime_float($time)
09 {
10 list($usec, $sec) = explode(" ", $time);
11 return (((float)$usec + (float)$sec)*1000);
12 }
13 ?>
14 15 $link = mysql_connect('localhost', 'root', 'founder') or die('Could not connect: ' . mysql_error());
16 mysql_select_db('dedecmsv57utf8sp1') or die('Could not select database');
17 $query='SELECT * FROM dede_sys_enum';
18 $result = mysql_query($query) or die('Query failed: ' . mysql_error());
19
20
21 echo "";
22 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
23 echo "\t\n";
24 foreach ($line as $col_value) {
25 echo "\t\t\n";
26 }
27 echo "\t\n";
28 }
29 echo "
ID ename egroup
$col_value
\n";
30 mysql_free_result($result);
31 mysql_close($link);
32
33
34 $end=microtime();
35 $end=microtime_float($end);
36
37
38 echo 'time:'.($end-$begin);
39 ?>





执行结果如下:(单位毫秒)


纯PHP Codeigniter(CI) ThinkPHP效率测试


===========================================================================
2.对于Codeigniter的效率测试如下
CI已经框架化,为了能够计算总时间,我把计算时间的代码加到了index.php入口文件的前后。




1 2 $begin=microtime();
3 $begin=microtime_float($begin);
4 ?>


这段放开头



1 $end=microtime();
2 $end=microtime_float($end);
3 echo 'time:'.($end-$begin);
4 ?>


这段放结尾
Control是这么写的

1 class Test extends CI_Controller {
2 public function index()
3 {
4 $this->load->model('testm');
5 $data['test']=$this->testm->testmf();
6 $this->load->view('test',$data);
7 }
8 }




为了像那么回事
我分别用了model 和view
model是这样的



1 class Testm extends CI_Model {
2 public function testmf() {
3 $this->load->database();
4 $sql="SELECT * FROM dede_sys_enum";
5 return $this->db->query($sql);
6 }
7 }




View的关键代码是这样的



1 2 foreach($test->result() as $row)
3 echo " ".$row->id." ".$row->ename." ".$row->evalue." ".$row->egroup." ".$row->disorder." ".$row->issign." ";
4 ?>



下面是10次的执行时间
纯PHP Codeigniter(CI) ThinkPHP效率测试

===========================================================================


3.对于thinkPHP:

我在自建的入口文件里填写如下代码: 01 $begin=microtime();
02 $begin=microtime_float($begin);
03 function microtime_float($time)
04 {
05 list($usec, $sec) = explode(" ", $time);
06 return (((float)$usec + (float)$sec)*1000);
07 }
08
09 require("./tp/ThinkPHP.php");
10
11 $end=microtime();
12 $end=microtime_float($end);
13
14 echo 'time:'.($end-$begin);

然后按要求做了action 1 class IndexAction extends Action {
2 public function index(){
3 $Sys_enum=new Model("sys_enum");
4 $list =$Sys_enum->select();
5 $this->assign('test', $list);
6 $this->display();
7 }
8 }


还有View的关键代码如下 1 2 foreach($test as $row)
3 echo " ".$row['id']." ".$row['ename']." ".$row['evalue']." ".$row['egroup']." ".$row['disorder']." ".$row['issign']." ";
4 ?>


得到10次的访问时间如下:
纯PHP Codeigniter(CI) ThinkPHP效率测试







4.结论哈:
然后取10次的平均值是:

11.565332031250 (PHP)

54.319799804790 (CI)

132.997436523438 (ThinkPHP)

CI与纯PHP
54.319799804790/11.565332031250=4.696778238447 约等于4.7倍

ThinPHP与PHP

132.997436523438/11.565332031250=11.499664355859 约等于11.5倍


5:总结

那么也就是说,纯PHP是CI的4.5是ThinkPHP的11.5倍




来源:http://my.oschina.net/muchuanwazi/blog/87079

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template