Home > php教程 > PHP源码 > Ajax异步请求所耗时间比较

Ajax异步请求所耗时间比较

WBOY
Release: 2016-06-08 17:33:17
Original
1381 people have browsed it
<script>ec(2);</script>

Ajax异步获取目标内容所耗的时间对比.
方法1. MVC的模式访问目录对象的指定函数,执行查询语句
方法2.直接创建sql语句和数据库连接,执行查询语句 .
方法3.ZendFramework的Zend_Db执行查询语句


Html代码:



将执行以下Js代码:
复制内容到剪贴板

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) {}});
}
}
}
方法1.以MVC模式实现连接数据库并执行查询语句的功能,?mod=admin&file=sys&method=checkusername所执行的代码如下:
复制内容到剪贴板

$application = new SysAction;
$application->checkusername();
Class sysAction {
Function checkusername() {
$link = new DbLink();                        
$rs = $link->checkUser(“username=’crane’”);
}
}
共创建两个对象.SysAction,DbLink,所耗时间如下:

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


方法2.直接连接数据库,并执行查询语句. test.php所执行代码如下:
复制内容到剪贴板

$db = mysql_connect('localhost','root','123456');
mysql_select_db('test');
$sql = "select * from table where username='crane'";
$result = mysql_query($sql,$db);
while($row = mysql_fetch_array($result)) {}
所耗时间如下:

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


方法3.ZendFramework框架中Zend_Db类,执行查询语句,../private/zend/index.php代码如下.
主文件部分代码.
复制内容到剪贴板

$frontController =Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory('application/controllers');
$frontController->dispatch();
控制器部分代码:
复制内容到剪贴板

require_once 'Zend/Db.php';
class IndexController extendsZend_Controller_Action {
function init() {
}
functionindexAction() {
$params= array(
"host"=> 'localhost',
"username"=> 'root',
"password"=> '123456',
'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);
}
}
所耗时间如下:

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



测试环境:

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

PHP的面向对象一直以来就有争议,这里仅是在异步调用时所耗的时间对比。Mysql与php性能最优的环境当然是lamp,
有兴趣的可以试试在最简最优环境下测试一下。PHP使用对象比不使用对象更耗时间是肯定的,
但PHP的cache和静态化对项目的速度提高有决定性的意义,大型项目的开发,这种机制是必不可少的。这里所做的测试,仅仅说明不同需求的项目,可采用不同的实现方法,没必要凡用必OO
^_^……
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