search
Homephp教程PHP源码Ajax异步请求所耗时间比较

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)