异步请求时间PHP,Ajax异步请求所耗时间比较-PHP源码

Ajax异步请求所耗时间比较-PHP源码

作者:小涵 | 来源:互联网 | 2018-07-17 15:20

阅读: 5319

ec(2);Ajax异步获取目标内容所耗的时间对比.方法1.MVC的模式访问目录对象的指定函数,执行查询语句方法2.直接创建sql语句和数据库连接,执行查询语句.方法3.ZendFramework的Zend_Db执行查询语句Html代码:<inputtype"text"name"username"id&quot

《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

^_^……

吐了个 "CAO" !

吐个槽吧,看都看了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值