最近一直想做一个技术类的新闻站点,想做的执行效率高些,想用PHP做,一直纠结于用纯PHP做还是用CI或者THINKPHP。用纯PHP效率高,缺点 n多,比如安全方面、构架方面等等等等;用CI、thinkPHP的话这些需要考虑的就少些,但是怕效率方面差得太多。后来就想先测测吧,如果差得太多, 就自己写个“框架”,满足自己的需求即可的框架。
CI版本是2.1.3,thinkphp用的是3.1。
因为大多数站点所做的事情就是查询数据库,因此此次的测试着重于数据库查询并显示。测试的数据库是dede的sys_enum,631条数据。目标就是查询出这些数据并显示到页面上计算消耗时间。
===========================================================================
1. 对于纯PHP,思路是在开始时记一下毫秒数,结束时记一下毫秒数,相减后得结果。纯PHP简单,直接贴代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?php
$begin
=microtime();
$begin
=microtime_float(
$begin
);
?>
<?php
function
microtime_float(
$time
)
{
list(
$usec
,
$sec
) =
explode
(
" "
,
$time
);
return
(((float)
$usec
+ (float)
$sec
)*1000);
}
?>
<?php
$link
= mysql_connect(
'localhost'
,
'root'
,
'founder'
)
or
die
(
'Could not connect: '
. mysql_error());
mysql_select_db(
'dedecmsv57utf8sp1'
)
or
die
(
'Could not select database'
);
$query
=
'SELECT * FROM dede_sys_enum'
;
$result
= mysql_query(
$query
)
or
die
(
'Query failed: '
. mysql_error());
echo
"<table width=\"416\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\"><tr><td>ID</td><td>ename</td><td>egroup</td></tr>"
;
while
(
$line
= mysql_fetch_array(
$result
, MYSQL_ASSOC)) {
echo
"\t<tr>\n"
;
foreach
(
$line
as
$col_value
) {
echo
"\t\t<td>$col_value</td>\n"
;
}
echo
"\t</tr>\n"
;
}
echo
"</table>\n"
;
mysql_free_result(
$result
);
mysql_close(
$link
);
$end
=microtime();
$end
=microtime_float(
$end
);
echo
'time:'
.(
$end
-
$begin
);
?>
|
执行结果如下:(单位毫秒)
===========================================================================
2.对于Codeigniter的效率测试如下
CI已经框架化,为了能够计算总时间,我把计算时间的代码加到了index.php入口文件的前后。
即
1
2
3
4
|
<?php
$begin
=microtime();
$begin
=microtime_float(
$begin
);
?>
|
这段放开头
1
2
3
4
|
$end
=microtime();
$end
=microtime_float(
$end
);
echo
'time:'
.(
$end
-
$begin
);
?>
|
这段放结尾
Control是这么写的
1
2
3
4
5
6
7
8
|
class
Test
extends
CI_Controller {
public
function
index()
{
$this
->load->model(
'testm'
);
$data
[
'test'
]=
$this
->testm->testmf();
$this
->load->view(
'test'
,
$data
);
}
}
|
为了像那么回事
我分别用了model 和view
model是这样的
1
2
3
4
5
6
7
|
class
Testm
extends
CI_Model {
public
function
testmf() {
$this
->load->database();
$sql
=
"SELECT * FROM dede_sys_enum"
;
return
$this
->db->query(
$sql
);
}
}
|
View的关键代码是这样的
1
2
3
4
|
<?php
foreach
(
$test
->result()
as
$row
)
echo
"<tr><td>"
.
$row
->id.
"</td><td>"
.
$row
->ename.
"</td><td>"
.
$row
->evalue.
"</td><td>"
.
$row
->egroup.
"</td><td>"
.
$row
->disorder.
"</td><td>"
.
$row
->issign.
"</td></tr>"
;
?>
|
下面是10次的执行时间
===========================================================================
3.对于thinkPHP:
我在自建的入口文件里填写如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$begin
=microtime();
$begin
=microtime_float(
$begin
);
function
microtime_float(
$time
)
{
list(
$usec
,
$sec
) =
explode
(
" "
,
$time
);
return
(((float)
$usec
+ (float)
$sec
)*1000);
}
require
(
"./tp/ThinkPHP.php"
);
$end
=microtime();
$end
=microtime_float(
$end
);
echo
'time:'
.(
$end
-
$begin
);
|
然后按要求做了action
1
2
3
4
5
6
7
8
|
class
IndexAction
extends
Action {
public
function
index(){
$Sys_enum
=
new
Model(
"sys_enum"
);
$list
=
$Sys_enum
->select();
$this
->assign(
'test'
,
$list
);
$this
->display();
}
}
|
还有View的关键代码如下
1
2
3
4
|
<?php
foreach
(
$test
as
$row
)
echo
"<tr><td>"
.
$row
[
'id'
].
"</td><td>"
.
$row
[
'ename'
].
"</td><td>"
.
$row
[
'evalue'
].
"</td><td>"
.
$row
[
'egroup'
].
"</td><td>"
.
$row
[
'disorder'
].
"</td><td>"
.
$row
[
'issign'
].
"</td></tr>"
;
?>
|
得到10次的访问时间如下:
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倍
我想还是我自己写个简单的框架吧,能够完成开发效率就OK的框架吧。