Recently, I have been trying to be a technical news site. I want to perform more efficiently and use PHP. I have been struggling to use pure PHP or CI or THINKPHP. It is highly efficient to use pure PHP with many disadvantages, such as security and architecture. If CI and thinkPHP are used, we need to consider less, but we are afraid that the efficiency is too low. I want to test it first.
Recently, I have been trying to be a technical news site. I want to perform more efficiently and use PHP. I have been struggling to use pure PHP or CI or THINKPHP. It is highly efficient to use pure PHP with many disadvantages, such as security and architecture. If CI and thinkPHP are used, we need to consider less, but we are afraid that the efficiency is too low. I want to test it first.
Recently, I have been trying to be a technical news site. I want to perform more efficiently and use PHP. I have been struggling to use pure PHP or CI or THINKPHP. It is highly efficient to use pure PHP with many disadvantages, such as security and architecture. If CI and thinkPHP are used, we need to consider less, but we are afraid that the efficiency is too low. Then I want to test it first. If there are too many differences, I will write a "Framework" to meet my own needs.
The CI version is 2.1.3, And the thinkphp version is 3.1.
Most websites query databases, so this test focuses on database query and display. The database to be tested is dede's sys_enum, with 631 pieces of data. The goal is to query the data and display it on the page to calculate the time consumption.
========================================================== ==========================================
1. For pure PHP, the idea is to remember the number of milliseconds at the beginning, and the number of milliseconds at the end, and the result after subtraction. PHP is simple and can be directly pasted with code:
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"
| ID |
Ename |
Egroup |
";22 while ($ line = mysql_fetch_array ($ result, MYSQL_ASSOC )){23 echo "\ t
\ N ";24 foreach ($ line as $ col_value ){25 echo "\ t
| $ Col_value | \ N ";26}27 echo "\ t
\ N ";28}29 echo"
\ 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?>
The execution result is as follows: (unit: milliseconds)
========================================================== ==========================================
2. test the efficiency of Codeigniter as follows:
CI has been modularized. In order to calculate the total time, I added the computing time code to the front and back of the index. php entry file.
That is
1 2 $ begin = microtime ();
3 $ begin = microtime_float ($ begin );
4?>
This section releases the header
1 $ end = microtime ();
2 $ end = microtime_float ($ end );
3 echo 'time: '. ($ end-$ begin );
4?>
End of this section
Control is written in this way.
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}
For something like that
I used model and view respectively.
The model is like this.
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}
The key code of View is as follows:
1 2 foreach ($ test-> result () as $ row)
3 echo"". $ Row-> id ."". $ Row-> ename ."". $ Row-> evalue ."". $ Row-> egroup ."". $ Row-> disorder ."". $ Row-> issign ."";
4?>
The following is the execution time of 10 times.
========================================================== ==========================================
3. For thinkPHP:
Enter the following code in the self-built portal file: 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 );
Then, the 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}
The key code of View is as follows: 1 2 foreach ($ test as $ row)
3 echo"". $ Row ['id']."". $ Row ['ename']."". $ Row ['evalue ']."". $ Row ['egroup']."". $ Row ['disorder']."". $ Row ['issign']."";
4?>
The following figure shows the 10 access times:
4. Conclusion:
Then the average value of 10 times is:
11.565332031250 (PHP)
54.319799804790 (CI)
132.997436523438 (ThinkPHP)
CI and pure PHP
54.319799804790/11.565332031250 = 4.696778238447 about 4.7 times
ThinPHP and PHP
132.997436523438/11.565332031250 = 11.499664355859 about 11.5 times
5. Summary
That is to say, pure PHP is 4.5 of CI and 11.5 times of ThinkPHP.
Source: http://my.oschina.net/muchuanwazi/blog/87079