This is a discussion on PHP Optimization Tips within the PHP Programming forums, part of the Web Development category; Hi, Set the maxvalue for your for-loops before and not in the loop. Code: $maxvalue = 100/10; for($i=...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, Set the maxvalue for your for-loops before and not in the loop. Code: $maxvalue = 100/10;
for($i=0; $i<$maxvalue; $i++){
// Some code
} Code: for($i=0; $i<100/10; $i++){
// Some code
} Regards, R.Kamalakannan. |
| Sponsored Links |
| |||
| if you want to assign variable to one of the array indexes, don’t do $var=$arr[‘foo’], better choice would be use references. $var=&$arr[‘foo’], now you just creating a reference to $arr[‘foo’] instead of duplicating it’s value. use $_SERVER[’REQUEST_TIME’] instead of every time calling time() function. For example declare constant at the config file: define(“TIME”, $_SERVER[’REQUEST_TIME’]) and use TIME when you want to know timestamp.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| ctype functions are faster than regular expressions. More info at PHP: Character Type Functions - Manual
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| - $arr[‘id’] works much more faster than $arr[id] - if you need to print a bigger piece of text without PHP variables just exit PHP (?>) and enter it when you need it again (<?php).
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi, Compiler/Opcode Caches ![]()
Thanks, R.Gopi. |
| |||
| Hi, Optimize the Web Server File I/O Operations
Thanks, R.Gopi. |
| |||
| Hi, Tuning PHP Configuration
Thanks, R.Gopi |
| |||
| Hi, Tips of Tuning PHP Including / File Accessing Whenever opening files or including scripts into the main script try to specify a full path or at least an easily resolvable partial path. Inefficient Approach: PHP Code: PHP Code: R.Gopi. |
| |||
| Hi, Optimizing the str_replace() The str_replace() function in PHP can be slow, due it’s duplication of data even if no replacement is being performed. PHP Code: Thanks, R.Gopi |
| |||
| HI, Web Server: Syscalls Syscall is function executed by the Kernel. The goal is to minimise the number of these calls needed to perform a request.
Thanks, R.Gopi. |
| |||
| Some more useful tips * printf is much more slower than echo or print, it must handle additional parameters. But echo is faster than print, despite of that both are constructors, print is returning boolean result, echo returns nothing. * when searching string in another string strstr() is faster than preg functions, however if you want to know if that string exists use strpos().
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| How many ways your web server can utilize PHP to generate web pages? Mainly there are three ways *************************** The first method is to use PHP as a CGI "wrapper". When run this way, an instance of the PHP interpreter is created and destroyed for every page request (for a PHP page) to your web server. Because it is destroyed after every request, any resources that it acquires (such as a link to an SQL database server) are closed when it is destroyed. In this case, you do not gain anything from trying to use persistent connections -- they simply don't persist. The second, and most popular, method is to run PHP as a module in a multiprocess web server, which currently only includes Apache. A multiprocess server typically has one process (the parent) which coordinates a set of processes (its children) who actually do the work of serving up web pages. When a request comes in from a client, it is handed off to one of the children that is not already serving another client. This means that when the same client makes a second request to the server, it may be served by a different child process than the first time. When opening a persistent connection, every following page requesting SQL services can reuse the same established connection to the SQL server. The last method is to use PHP as a plug-in for a multithreaded web server. Currently PHP 4 has support for ISAPI, WSAPI, and NSAPI (on Windows), which all allow PHP to be used as a plug-in on multithreaded servers like Netscape FastTrack (iPlanet), Microsoft's Internet Information Server (IIS), and O'Reilly's WebSite Pro. The behavior is essentially the same as for the multiprocess model described before. Note that SAPI support is not available in PHP 3.
__________________ Thanks & Regards Sabari... |
| |||
| PHP is a very fast programming language, but there is more to optimizing PHP than just speed of code execution. here, we explain why optimizing PHP involves many factors which are not code related, and why tuning PHP requires an understanding of how PHP performs in relation to all the other subsystems on your server, and then identifying bottlenecks caused by these subsystems and fixing them. We also cover how to tune and optimize your PHP scripts so they run even faster.
__________________ Thanks & Regards Sabari... |
| |||
| Achieving High Performance When we talk about good performance, we are not talking about how fast your PHP scripts will run. Performance is a set of tradeoffs between scalability and speed. Scripts tuned to use fewer resources might be slower than scripts that perform caching, but more copies of the same script can be run at one time on a web server. In the example below, A.php is a sprinter that can run fast, and B.php is a marathon runner than can jog forever at the nearly the same speed. For light loads, A.php is substantially faster, but as the web traffic increases, the performance of B.php only drops a little bit while A.php just runs out of steam. ![]() Let us take a more realistic example to clarify matters further. Suppose we need to write a PHP script that reads a 250K file and generates a HTML summary of the file. We write 2 scripts that do the same thing: hare.php that reads the whole file into memory at once and processes it in one pass, and tortoise.php that reads the file, one line at time, never keeping more than the longest line in memory. Tortoise.php will be slower as multiple reads are issued, requiring more system calls. Hare.php requires 0.04 seconds of CPU and 10 Mb RAM and tortoise.php requires 0.06 seconds of CPU and 5 Mb RAM. The server has 100 Mb free actual RAM and its CPU is 99% idle. Assume no memory fragmentation occurs to simplify things. At 10 concurrent scripts running, hare.php will run out of memory (10 x 10 = 100). At that point, tortoise.php will still have 50 Mb of free memory. The 11th concurrent script to run will bring hare.php to its knees as it starts using virtual memory, slowing it down to maybe half its original speed; each invocation of hare.php now takes 0.08 seconds of CPU time. Meanwhile, tortoise.php will be still be running at its normal 0.06 seconds CPU time.
__________________ Thanks & Regards Sabari... |
| |||
| In the table below, the faster php script for different loads is in bold: ---------------------------------------------------------------------------------------------- Connections | CPU seconds required to satisfy 1 HTTP request | CPU seconds required to satisfy 10 HTTP requests | CPU seconds required to satisfy 11 HTTP requests | ---------------------------------------------------------------------------------------- hare.php | 0.04 | 0.40 | 0.88 (runs out of RAM) ---------------------------------------------------------------------------------------- tortoise.php | 0.06 | 0.60 | 0.66 ---------------------------------------------------------------------------------------- As the above example shows, obtaining good performance is not merely writing fast PHP scripts. High performance PHP requires a good understanding of the underlying hardware, the operating system and supporting software such as the web server and database.
__________________ Thanks & Regards Sabari... Last edited by Sabari : 11-14-2007 at 11:01 PM. |
| |||
| Bottlenecks The hare and tortoise example has shown us that bottlenecks cause slowdowns. With infinite RAM, hare.php will always be faster than tortoise.php. Unfortunately, the above model is a bit simplistic and there are many other bottlenecks to performance apart from RAM:
__________________ Thanks & Regards Sabari... |
| |||
| (a) Networking Your network is probably the biggest bottleneck. Let us say you have a 10 Mbit link to the Internet, over which you can pump 1 megabyte of data per second. If each web page is 30k, a mere 33 web pages per second will saturate the line. More subtle networking bottlenecks include frequent access to slow network services such as DNS, or allocating insufficient memory for networking software.
__________________ Thanks & Regards Sabari... |
| |||
| (b) CPU If you monitor your CPU load, sending plain HTML pages over a network will not tax your CPU at all because as we mentioned earlier, the bottleneck will be the network. However for the complex dynamic web pages that PHP generates, your CPU speed will normally become the limiting factor. Having a server with multiple processors or having a server farm can alleviate this.
__________________ Thanks & Regards Sabari... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/4357-php-optimization-tips.html | |||
| Posted By | For | Type | Date |
| The Storyteller | This thread | Refback | 11-30-2007 07:09 AM |
| del.icio.us/subscriptions/KoerPoiss | This thread | Refback | 11-30-2007 06:47 AM |
| Planet PHP Japan | This thread | Refback | 11-30-2007 04:35 AM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |