Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> Php
 
Benchmark And Optimize PHP Script Speed
 

I found out the hard way why speed optimization in PHP is very important. Part of my work is developing a very complex digital learning system and this product has had it's first run a while ago. During the time the users in the system had grown from more than 3000 and more than 8000 usergroups. When this happened we found out the system didn't handle large numbers of users very well.

Through this, i learned that it is very important to start optimizing your code from the beginning of the project and apply a few best practices to speed up a script. A script that takes 0,01 second doesn't seem very long, but if you multiply this by 8000, it takes 80 seconds (!!!) to render a page...

Benchmark

In my experience it is the hardest part to find which part of a existing script is slowing the page down. In my case i thought it was somewhere at the end of the page, but later it turned out to be in the beginning of the page, when initializing the classes. Looking in the wrong place can cost you a lot of time!

One option is to comment everything in a page, and uncomment all the lines one by one. When the page slows down, you found the line where the slow part is.

But sometimes this isn't enough (not in my case) and you want to know how long a particular part of the script takes to execute. The second option is to divide the script in parts (not literally) and time the parts. If that isn't enough you can place a timer "around" every function or line, to get exact details on which part of the page takes how much time to execute.

I use this script to do this:

PHP

function getTime() {
    $timer = explode( ' ', microtime() );
    $timer = $timer[1] + $timer[0];
    return $timer;
}

And in a sample of code i use it this way:

PHP

$start = getTime();
    $aUsers = $wms->getUsers( 0, $showType, true, ($_REQUEST['page']*$perPage), $perPage );
$end = getTime();
echo  '<strong>getUsers/getUsersByAbc</strong>: '.round($end - $start,4).' seconden<br />';

In line 1 the starting time is stored in $start. In line 2 the funciton is executed and in line 3 the ending time is stored in $end. At last in line 4 a string is displayed with the time it took to execute the function (rounded at 4 decimals).

Optimizing

Now you found where the slowness is, you can apply these best practices:

  • Use static methods when possible, this is 4 times faster
  • Echo is faster as print
  • Use , instead of . to concatinate a string
  • Set the max value in a for-loop before the loop, not in the if statement.
    $max = count($array);
    for ($i=0;$i<$max;$i++) {
    echo $i;
    }
  • Unset vars to clear memory (especially when using arrays)
  • Use full paths in includes and requires, so the server doesn't have to resolve the paths for you
  • Use strncasecmp, strpbrk and stripos in stead of regex
  • It's better to use a select statement then multiple if statements with multiple else statements
  • Suppress errors with an @ is very slow
  • Close database connections when you don't need them anymore
  • $row['id'] is 7 times faster as $row[id]
  • Incrementing a global var is 2 times slower as a local var
  • Wrap your string in single quotes (') instead of double quotes (") is faster because PHP searches for vars in "..." and not in '..'
  • A PHP script is 2 to 20 times slower as a HTML page. Use HTML pages when possible.
  • PHP scripts are compiled everytime you call them. Use PHP caching software to gain a speed optimization of 25% to 100%.
  • $i++ is slower as ++$i
  • Not everything has to be OOP. Most of the time OOP is just overhead, so use it wisely!
  • Use as much of the default functions of PHP, you don't have te reinvent the wheel!

I found this list with best practices on the internet and printed it out once, but i can't find the source of it.

 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› ASP.NET Simple File Upload Function

› How to Send an Email in Asp.Net

› Get To The Top 10 Website Search Engine Rank In 5 Easy Steps

› Intro To Object: Option Variables

› Displaying Data using ASP.NET 2.0 Repeater

› Storing and Retrieving Variables From Application Object - Asp

› Writing SEO Friendly URL using HttpHandlers in ASP.NET

› Cookies in ASP.NET

› Create Tell A Friend Script With HTML & PHP

› Ajax - The Basics

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.