首页 > php代码 > Ajax异步请求所耗时间比较

Ajax异步请求所耗时间比较


Ajax异步获取目标内容所耗的时间对比.

方法1. MVC的模式访问目录对象的指定函数,执行查询语句 

方法2.直接创建sql语句和数据库连接,执行查询语句 .

方法3.ZendFramework的Zend_Db执行查询语句



Html代码:

<input type="text"name="username" id="username" onblur='cek.checkUser()'/>



将执行以下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&rsquo;;
            //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所执行的代码如下:

复制内容到剪贴板

<?php

$application = new SysAction;

$application->checkusername();

Class sysAction {

Function checkusername() {

$link = new DbLink();                        

$rs = $link->checkUser(&ldquo;username=&rsquo;crane&rsquo;&rdquo;);

}

}

共创建两个对象.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

^_^&hellip;&hellip;


本文地址:http://www.phprm.com/code/5d0c9e890cdf60c1c9583a0734f0fabc.html

转载随意,但请附上文章地址:-)

标签:none

发表留言