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
^_^……