Time-consuming comparison of Ajax asynchronous requests-PHP source code

Source: Internet
Author: User
Ec (2); time consumption of Ajax asynchronous retrieval of target content. method 1. in MVC mode, access the specified function of the directory object and execute the query statement. Method 2. directly create SQL statements and database connections and execute query statements. method 3. the Zend_Db of ZendFramework executes the query statement Html code: & lt; inputtype & quot; text & quot; name & quot; username & quot; id & quot script ec (2 ); script


The time used by Ajax to asynchronously obtain the target content is compared.
Method 1. Access the specified function of the directory object in MVC mode and execute the query statement.
Method 2: directly create SQL statements and database connections and execute query statements.
Method 3: run the query statement in Zend_Db of ZendFramework.


Html code:



The following Js Code will be executed:
Copy content to clipboard

Var cek = {
CheckUser: function (user ){
If (! $ ('Modify'). value | $ ('default _ user'). value! = $ (User). value ){
Var url = "? Mod = admin & file = sys & method = checkusername ";
// Var url = 'test. php ';
// Var url = '../private/zend/index. php ';
Var pars = '';
Var myAjax = new Ajax. Request (url, {method: 'get', parameters: pars, onComplete: function (contents ){}});
}
}
}
Method 1: Use the MVC mode to connect to the database and execute query statements ,? The Code executed by mod = admin & file = sys & method = checkusername is as follows:
Copy content to clipboard

$ Application = new SysAction;
$ Application-> checkusername ();
Class sysAction {
Function checkusername (){
$ Link = new DbLink ();
$ Rs = $ link-> checkUser ("username = 'crane '");
}
}
Create two objects. SysAction and DbLink. the time consumed is as follows:

GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (63 ms)
GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (62 ms)
GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (62 ms)
GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (62 ms)
GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (62 ms)
GET http: // localhost/admin /? Mod = admin & file = sys & method = checkusername (63 ms)


Method 2: directly connect to the database and execute the query statement. test. php. The Execution Code is as follows:
Copy content to clipboard

$ Db = mysql_connect ('localhost', 'root', '123 ');
Mysql_select_db ('test ');
$ SQL = "select * from table where username = 'crane '";
$ Result = mysql_query ($ SQL, $ db );
While ($ row = mysql_fetch_array ($ result )){}
The time consumed is as follows:

GET http: // localhost/admin/test. php (15 ms)
GET http: // localhost/admin/test. php (15 ms)
GET http: // localhost/admin/test. php (15 ms)
GET http: // localhost/admin/test. php (15 ms)
GET http: // localhost/admin/test. php (15 ms)
GET http: // localhost/admin/test. php (15 ms)


Method 3: run the query statement in the Zend_Db class in the ZendFramework. The code for ../private/zend/index. php is as follows.
The code of the main file.
Copy content to clipboard

$ FrontController = Zend_Controller_Front: getInstance ();
$ FrontController-> throwExceptions (true );
$ FrontController-> setControllerDirectory ('application/controllers ');
$ FrontController-> dispatch ();
Controller code:
Copy content to clipboard

Require_once 'zend/Db. php ';
Class IndexController extendsZend_Controller_Action {
Function init (){
}
FunctionindexAction (){
$ Params = array (
"Host" => 'localhost ',
"Username" => 'root ',
"Password" => '123 ',
'Dbname' => 'test'
);
// $ User = 'crane ';
$ Db = Zend_Db: factory ('pdo _ mysql', $ params );
$ Select = $ db-> select ();
$ Select-> from ('table', 'username ')
-> Where ($ db-> quoteInto ('username =? ', 'Crane '))
;
$ SQL = $ select->__ toString ();
$ Result = $ db-> fetchAll ($ SQL );
}
}
The time consumed is as follows:

GET http: // localhost/private/zend/index. php (125 ms)
GET http: // localhost/private/zend/index. php (141 ms)
GET http: // localhost/private/zend/index. php (110 ms)
GET http: // localhost/private/zend/index. php (141 ms)
GET http: // localhost/private/zend/index. php (109 ms)
GET http: // localhost/private/zend/index. php (141 ms)



Test environment:

Windows NT 5.1 build 2600
In Apache 2.0
PHP 5.2.5
Mysql 5.0.45
Zend Optimizer v3.3.0

PHP Object-oriented has always been controversial. Here we only compare the time spent in asynchronous calls. The environment with optimal performance for Mysql and php is lamp, of course,
If you are interested, try testing in the simplest and optimal environment. PHP is certainly more time-consuming than not using objects,
However, the cache and static nature of PHP play a decisive role in improving the speed of the project. This mechanism is essential for the development of large projects. The tests made here only describe the projects with different requirements. Different implementation methods can be used. It is not necessary to use them.
Pai_^ ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.