- 浏览: 378308 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
周仁明:
xin911 写道周仁明 写道js的借用了,谢谢!java的版 ...
人民币金额数字转中文大写程序多种编程语言汇总2011版 -
xin911:
周仁明 写道js的借用了,谢谢!java的版本必然是不对的问题 ...
人民币金额数字转中文大写程序多种编程语言汇总2011版 -
周仁明:
js的借用了,谢谢!java的版本必然是不对的问题很多。
人民币金额数字转中文大写程序多种编程语言汇总2011版 -
zhangzhj85:
...
公开几个移动互联产品设计大神的观点 -
white_crucifix:
嗯,不过网络诊断功能能帮上忙的次数的确微乎其微
戏说windows 7 中的优秀设计
Benchmarking Autoload Performance In PHP
by Sam on October 18, 2009 · 8 comments
in PHP/MySQL
I read a blog post a few days ago written by Brandon Savage outlining some of the more worthwhile optimizations one can make to their PHP code and application environment in order to improve performance. One of the optimizations suggested by Brandon is employing the use of an autoloader (for the uninitiated, you can read all about autoloading here). On reading through the comments, I noticed that there seemed to be some uncertainty as to how autoloading stacks up against the use of multiple include files as it relates to speed. After running a quick Google search on the topic, I realized that the uncertainty extended far beyond Brandon’s article. I decided then, to do some benchmarking of my own and post the results.
First of all, let me categorically state that for reasons which I’ll reveal in a later article, I don’t care very much for benchmarks. These tests were carried out merely out of academic interest. Secondly, I am a fan of anything which makes my life easier. Autoloading makes my life easier. Enough said.
The Machine
These tests were carried out on a Sony Vaio notebook with 4GB of RAM and a core 2 duo processor with each core running at 2.0Ghz. The software versions are listed below:
■Windows 7 RC
■PHP 5.3.0
■Apache 2.2.12
■eAccelerator 0.9.6 RC1
The Tests
Two sets of tests were carried out, one without opcode caching and one with opcode caching (courtesy of eAccelerator). Each set of tests include tests using multiple include() calls, tests using the SPL autoloader and tests using the __autoload magic function.
The Code
The code used to perform the tests is extremely simple. It consists of a timer class as defined below:
[code=php]
class Timer
{
private $totalTime;
public function startTimer() {
$this->totalTime = microtime(true);
}
public function stopTimer() {
$this->totalTime = round(microtime(true)-$this->totalTime,8);
}
public function getTime() {
return $this->totalTime;
}
}
As you can see, the Timer class has Timer::startTimer() and Timer::stopTimer() methods which start and stop the timer respectively. Calls to these methods envelop the code which we are evaluating, i.e. the multiple includes or our autoload functions:
[code=php]
$timer = new Timer;
$timer->startTimer();
//Code block used to test multiple includes.
include 'includes/dummy.class.php';
include 'includes/dummytwo.class.php';
include 'includes/dummythree.class.php';
include 'includes/dummyfour.class.php';
include 'includes/dummyfive.class.php';
//Code block used to test SPL Autoloader.
function autoload($classname) {
$name = strtolower($classname);
include 'includes' . DIRECTORY_SEPARATOR . $name . '.class.php';
}
spl_autoload_register('autoload');
//Code block used to test __autoload()
function __autoload($classname) {
$name = strtolower($classname);
include 'includes' . DIRECTORY_SEPARATOR . $name . '.class.php';
}
$dummyOne = new Dummy;
$dummyTwo = new DummyTwo;
$dummyThree = new DummyThree;
$dummyFour = new DummyFour;
$dummyFive = new DummyFive;
$timer->stopTimer();
echo $timer->getTime();
The classes being included are identical save for their names and simply perform a mathematical addition 50 times each:
[code=php]
class Dummy
{
public function __construct() {
for($i=0;$i<50;$i++) {
$r = $i + 1;
}
}
}
For tests done while using eAccelerator, the relevant php.ini settings were as follows:
[eAccelerator]
eaccelerator.shm_size = "0"
eaccelerator.enable = "1"
eaccelerator.debug = 0
eaccelerator.check_mtime = "1"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "0"
eaccelerator.shm_prune_period = "0"
eaccelerator.shm_only = "0"The Results
Now onto the results. Take a look:
PHP 5.3.0 Without Opcode Caching
Multiple include() SPL Autoloader __autoload()
Trial 1 0.00168896 0.00130391 0.00112915
Trial 2 0.00153303 0.00128794 0.00122499
Trial 3 0.00114799 0.00116611 0.00140595
Trial 4 0.00140595 0.00124097 0.00120497
Trial 5 0.00173497 0.00115585 0.00128889
Trial 6 0.00110888 0.00119805 0.00119996
Average (seconds) 0.00143663 0.001225472 0.001242318
PHP 5.3.0 With Opcode Caching (eAccelerator)
Multiple include() SPL Autoloader __autoload()
Trial 1 0.00052309 0.00060201 0.00057101
Trial 2 0.00049305 0.00054383 0.00055003
Trial 3 0.00049591 0.00055194 0.00060415
Trial 4 0.00049996 0.00055408 0.00054097
Trial 5 0.00052595 0.00058699 0.00054979
Trial 6 0.0005219 0.00055003 0.00053716
Average (seconds) 0.000509977 0.000564813 0.000558852
The first set of tests indicate that autoloading is faster than multiple includes. The SPL autoloader wins here with the __autoload() function coming in second and multiple includes quite a way behind in third. The second set of tests however convey the complete opposite. Here we see multiple includes winning out by a clear margin with the __autoload() function coming in second and the SPL autoloader finishing third. I was quite interested to find out why using multiple includes is faster than autoloading when using an opcode caching system and so I did a bit of research. Apparently some opcode caching systems may have issues with caching autoloaded classes. I’m going to have to do a bit more digging to find out why exactly that is.
These tests can be modified and extended in a number of ways to improve the data collected. They could be carried out on Unix, Linux and Mac systems, they could be modified so they test against require(), require_once() and include_once() or they could be tested with differing classes. This data is simply a starting point and is subject to one of the biggest issues I have with benchmarks, they simply don’t effectively mirror real world systems.
Final Thoughts
We have one victory here for the pro-autoload developers and one for the anti-autoload developers. These tests and results however provide quite an effective distraction from the entire point of using an autoloader. Even if autoloading proved to be whole seconds slower than using multiple includes, it is a technique I’d still use. Religiously. Why you ask? The answer is simple. The benefits of using an autoloader are not centered around speed gains. It is about organizational efficiency. Autoloading allows you to write less lines of code, which in turn decreases the probability of bugs creeping up in your code. Think about it, writing less code equals less opportunity to mess up somewhere. Additionally, in cases where you might include 25 files of which only 5 are used autoloading would clearly prove to be the faster option. Somewhere in the region of 5 times faster I’m guessing. Cleaner, smaller and in some cases faster code. That is what autoloading offers. If these results teach us anything, it is that we should definitely use an opcode caching system on our servers. The difference in speed between the two sets of tests is incredible!
I am open to any comments or criticism regarding how these tests were carried out, and I welcome any data which validates or invalidates my own. I have yet to perform these tests on a Linux server, however once I get around to upgrading to PHP 5.3 on my Linux box I’ll be sure to rerun the tests and post the results. I hope that you found this article useful.
by Sam on October 18, 2009 · 8 comments
in PHP/MySQL
I read a blog post a few days ago written by Brandon Savage outlining some of the more worthwhile optimizations one can make to their PHP code and application environment in order to improve performance. One of the optimizations suggested by Brandon is employing the use of an autoloader (for the uninitiated, you can read all about autoloading here). On reading through the comments, I noticed that there seemed to be some uncertainty as to how autoloading stacks up against the use of multiple include files as it relates to speed. After running a quick Google search on the topic, I realized that the uncertainty extended far beyond Brandon’s article. I decided then, to do some benchmarking of my own and post the results.
First of all, let me categorically state that for reasons which I’ll reveal in a later article, I don’t care very much for benchmarks. These tests were carried out merely out of academic interest. Secondly, I am a fan of anything which makes my life easier. Autoloading makes my life easier. Enough said.
The Machine
These tests were carried out on a Sony Vaio notebook with 4GB of RAM and a core 2 duo processor with each core running at 2.0Ghz. The software versions are listed below:
■Windows 7 RC
■PHP 5.3.0
■Apache 2.2.12
■eAccelerator 0.9.6 RC1
The Tests
Two sets of tests were carried out, one without opcode caching and one with opcode caching (courtesy of eAccelerator). Each set of tests include tests using multiple include() calls, tests using the SPL autoloader and tests using the __autoload magic function.
The Code
The code used to perform the tests is extremely simple. It consists of a timer class as defined below:
[code=php]
class Timer
{
private $totalTime;
public function startTimer() {
$this->totalTime = microtime(true);
}
public function stopTimer() {
$this->totalTime = round(microtime(true)-$this->totalTime,8);
}
public function getTime() {
return $this->totalTime;
}
}
As you can see, the Timer class has Timer::startTimer() and Timer::stopTimer() methods which start and stop the timer respectively. Calls to these methods envelop the code which we are evaluating, i.e. the multiple includes or our autoload functions:
[code=php]
$timer = new Timer;
$timer->startTimer();
//Code block used to test multiple includes.
include 'includes/dummy.class.php';
include 'includes/dummytwo.class.php';
include 'includes/dummythree.class.php';
include 'includes/dummyfour.class.php';
include 'includes/dummyfive.class.php';
//Code block used to test SPL Autoloader.
function autoload($classname) {
$name = strtolower($classname);
include 'includes' . DIRECTORY_SEPARATOR . $name . '.class.php';
}
spl_autoload_register('autoload');
//Code block used to test __autoload()
function __autoload($classname) {
$name = strtolower($classname);
include 'includes' . DIRECTORY_SEPARATOR . $name . '.class.php';
}
$dummyOne = new Dummy;
$dummyTwo = new DummyTwo;
$dummyThree = new DummyThree;
$dummyFour = new DummyFour;
$dummyFive = new DummyFive;
$timer->stopTimer();
echo $timer->getTime();
The classes being included are identical save for their names and simply perform a mathematical addition 50 times each:
[code=php]
class Dummy
{
public function __construct() {
for($i=0;$i<50;$i++) {
$r = $i + 1;
}
}
}
For tests done while using eAccelerator, the relevant php.ini settings were as follows:
[eAccelerator]
eaccelerator.shm_size = "0"
eaccelerator.enable = "1"
eaccelerator.debug = 0
eaccelerator.check_mtime = "1"
eaccelerator.filter = ""
eaccelerator.shm_max = "0"
eaccelerator.shm_ttl = "0"
eaccelerator.shm_prune_period = "0"
eaccelerator.shm_only = "0"The Results
Now onto the results. Take a look:
PHP 5.3.0 Without Opcode Caching
Multiple include() SPL Autoloader __autoload()
Trial 1 0.00168896 0.00130391 0.00112915
Trial 2 0.00153303 0.00128794 0.00122499
Trial 3 0.00114799 0.00116611 0.00140595
Trial 4 0.00140595 0.00124097 0.00120497
Trial 5 0.00173497 0.00115585 0.00128889
Trial 6 0.00110888 0.00119805 0.00119996
Average (seconds) 0.00143663 0.001225472 0.001242318
PHP 5.3.0 With Opcode Caching (eAccelerator)
Multiple include() SPL Autoloader __autoload()
Trial 1 0.00052309 0.00060201 0.00057101
Trial 2 0.00049305 0.00054383 0.00055003
Trial 3 0.00049591 0.00055194 0.00060415
Trial 4 0.00049996 0.00055408 0.00054097
Trial 5 0.00052595 0.00058699 0.00054979
Trial 6 0.0005219 0.00055003 0.00053716
Average (seconds) 0.000509977 0.000564813 0.000558852
The first set of tests indicate that autoloading is faster than multiple includes. The SPL autoloader wins here with the __autoload() function coming in second and multiple includes quite a way behind in third. The second set of tests however convey the complete opposite. Here we see multiple includes winning out by a clear margin with the __autoload() function coming in second and the SPL autoloader finishing third. I was quite interested to find out why using multiple includes is faster than autoloading when using an opcode caching system and so I did a bit of research. Apparently some opcode caching systems may have issues with caching autoloaded classes. I’m going to have to do a bit more digging to find out why exactly that is.
These tests can be modified and extended in a number of ways to improve the data collected. They could be carried out on Unix, Linux and Mac systems, they could be modified so they test against require(), require_once() and include_once() or they could be tested with differing classes. This data is simply a starting point and is subject to one of the biggest issues I have with benchmarks, they simply don’t effectively mirror real world systems.
Final Thoughts
We have one victory here for the pro-autoload developers and one for the anti-autoload developers. These tests and results however provide quite an effective distraction from the entire point of using an autoloader. Even if autoloading proved to be whole seconds slower than using multiple includes, it is a technique I’d still use. Religiously. Why you ask? The answer is simple. The benefits of using an autoloader are not centered around speed gains. It is about organizational efficiency. Autoloading allows you to write less lines of code, which in turn decreases the probability of bugs creeping up in your code. Think about it, writing less code equals less opportunity to mess up somewhere. Additionally, in cases where you might include 25 files of which only 5 are used autoloading would clearly prove to be the faster option. Somewhere in the region of 5 times faster I’m guessing. Cleaner, smaller and in some cases faster code. That is what autoloading offers. If these results teach us anything, it is that we should definitely use an opcode caching system on our servers. The difference in speed between the two sets of tests is incredible!
I am open to any comments or criticism regarding how these tests were carried out, and I welcome any data which validates or invalidates my own. I have yet to perform these tests on a Linux server, however once I get around to upgrading to PHP 5.3 on my Linux box I’ll be sure to rerun the tests and post the results. I hope that you found this article useful.
发表评论
-
最好的编程语言及其它
2020-10-18 19:48 592一提起最好的编程语言,PHPer一定会说,PHP是世界上最好 ... -
Sight——杀手级提升Laravel开发速度的组件现在开源了!
2020-10-11 18:01 361Sight——杀手级提升Laravel开发速度的组件现在开源 ... -
PHP迭代器模式与环形链表
2020-08-02 15:07 370迭代器模式,并不在最初的23个设计模式中。但 ... -
喜报:我中了一个巨额特等奖,关于PHP的。
2020-07-15 03:53 3今天写程序,程序中需要计算并检查计算的结果 ... -
PHP启动出错,不能加载扩展。
2020-05-10 14:36 791PHP Warning: PHP Startup: Un ... -
PHP中强制引用的实现
2020-01-09 23:17 459为什么要使用引 ... -
PHP数组函数(按使用频度排序)
2019-09-14 17:39 632PHP数组函数较多,初学者很多都记不住,经常会要用时临时查手 ... -
FastAdmin系统后台存在高危安全漏洞
2019-09-07 23:27 2365FastAdmin系统后台存在高危安全漏洞 FastAdm ... -
PHP流数据动态结构处理包简介
2019-09-07 23:11 638SDDS(Stream Data Dynamic Stru ... -
推荐一个Socket 应用协议层组件
2019-06-06 00:34 922SDDS(Stream Data Dynamic Struc ... -
PHP典型垃圾代码点评(1)
2019-05-25 00:48 763从今天开始,陆续给 ... -
Php-Redis-Admin完全安装指南
2019-03-02 13:09 617Php-Redis-Admin 是基于PhpRedmin的 ... -
PHP:在对象上动态添加一个新的方法
2015-02-06 00:44 4306有关在一个对象上动态添加方法,如果你来自Ruby语言或您熟 ... -
数据库变更管理系统ruckusing
2013-09-22 17:38 1674源代码变更管理,我们用CVS,SVN,GITHUB,SOOU ... -
元数据驱动还是标签引擎?
2013-07-22 01:48 2044——OpenBiz Cubi试用随 ... -
推荐 EPESI开发框加与CRM/ERP
2012-12-10 00:50 1817PHP的EPESI开发框架,可能很多人并不熟悉。 ... -
关于PHP的工作流引擎EZER简介
2012-11-30 00:30 22534关于PHP的工 ... -
使用PHP发送传真(PHP code for Sending fax)
2012-04-17 12:58 2883到目前,国内未找到如何用PHP发传真的文章,ITEYE ... -
WEB开发编码规范三字经
2012-02-09 00:32 1327以下是一个仅72字的编码规范三字经。虽说是针对WEB开发而定的 ... -
PHP的连续赋值
2011-08-15 13:20 5912连续赋值很多编程语言均支持,比如:JAVASCRIPT ...
相关推荐
Quantitative Models for Performance Evaluation and Benchmarking Data Envelopment Analysis with Spreadsheets 数据包络分析:绩效评估与标杆
Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed., At the end, the book discusses some best practices in PHP programming ...
Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed. At the end, the book discusses some best practices in PHP programming ...
本部分所提及的书名为《Benchmarking with DEA, SFA, and R》,其内容侧重于讲述企业效率评价的最新方法,尤其是数据包络分析(Data Envelopment Analysis, DEA)和随机前沿分析(Stochastic Frontier Analysis, SFA...
The accelerating deployment of large-scale web, cloud, Big Data, and virtualized computing systems has introduced serious new challenges in performance optimization. Until now, however, little ...
and benchmarking Interpret performance data to analyze your Linux server's real-world behavior Optimize Linux system schedulers, memory, I/O, file systems, and networking Tune web, file, database, and...
Starting with performance analysis concepts and their importance in helping Node.js developers eliminate performance bottlenecks, this book will take you through development patterns to avoid ...
It will expertly guide you through the nuts and bolts of extreme performance optimization in .NET, complete with in-depth examinations of CLR functionality, free tool recommendations and tutorials, ...
Written by noted experts with years of real-world experience building very large systems, this book covers every aspect of MySQL performance in detail, and focuses on robustness, security, and data ...
you will learn how to profile your PHP scripts with Blackfire.io, monitor your Web applications, measure database performance, optimize SQL queries, explore Functional JavaScript, boost Web server ...
The book also includes chapters on benchmarking, profiling, backups, security, and tools and techniques to help you measure, monitor, and manage your MySQL installations. HomePage: ...
### 图像二值化技术及定量性能评估 #### 引言 在图像处理的许多应用中,属于目标对象的像素灰度与背景像素的灰度存在显著差异。因此,二值化成为一种简单而有效的工具,用于将目标物体与背景分离。...
Red Hat Enterprise Performance Tuning ...system performance, performance adjustments, open source benchmarking utilities, networking performance, and tuning configurations for specific application loads.
We’ll introduce you to the concepts of Object-Oriented Programming (OOP) in PHP 7, then shed some light on how to improve the performance of your PHP 7 applications and database. Throughout this ...