TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-29-2007, 05:36 AM   #1 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default Tips: Fastest PHP Code

Hey everyone!

We all know that there are some methods in PHP that are just damn slow... But there are others we don't know are slowing us down.

When it comes down to it, any speed saved is good as it also saves CPU cycles and potentially saves memory (though not always the case). I would like everyone to post their speed tests here. We can make a list of methods and coding styles that are the fastest. =)

Just so we're all on the same page, we should use the same speed function. this is what I have:

PHP Code:
function MicrotimeFloat() { // from php.net/microtime
    
list($usec$sec) = explode(" "microtime());
    return ((float)
$usec + (float)$sec);
}


function 
GetTimeDifference($time1,$time2) {
    return 
number_format( ($time2 $time1) , 2);
}

// then i will use:

$StartTime MicrotimeFloat();
// do stuff here to time
$EndTime MicrotimeFloat();
echo 
"This took this many seconds: "GetTimeDifference($StartTime$EndTime); 

First off the bat, I'm going to do string concatenation. These are the 4 methods I'll test (On PHP Version 5.2.2):

PHP Code:
$strInputString 'This string is inside another string';

$strMethodOne "This is my string " $strInputString " another section";
$strMethodTwo "This is my string $strInputString another section";
$strMethodThree "This is my string {$strInputString} another section";
$strMethodFour sprintf("This is my string %s another section",  $strInputString ); 
I looped each one 1,000,000 times. These are the resilts I got (4 times each):

Method One: 0.34, 0.38, 0.32, 0.32 secs
Method Two: 0.41, 0.40, 0.41, 0.40 secs
Method Three: 0.39, 0.42, 0.41, 0.42 secs
Method Four: 0.99, 0.98, 0.99, 0.98 secs

Averages: (rounded to 2 places)
Method One: 0.34 secs Using: "s".$str."s";
Method Two: 0.41 secs Using: "s $str s";
Method Three: 0.41 secs Using: "s {$str} s";
Method Four: 0.99 secs Using: sprintf()

Using sprintf is by far the slowest here. I wasn't a fan of sprintf anyway, so that was kinda a weight off my shoulder. The best seems to be the old dot method, which takes about 1/3 the time that sprintf does.

Though, this will obviously not make a significant impact on your script either way (notice its a million times just for those low speeds!), it's good to know which is actually quickest.

I have to run off atm, but I'll post more speed tests later. Also if anyone else has any, post here, it'd be great to have a large collection for everyone to reference.

Oh and if you think I've done something wrong with my method here, let me know. :)
jordie is offline  
Reply With Quote
Old 09-29-2007, 07:55 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Good call! Using your exact same code and looping 1 million times, I pitted eregi against preg_match with the case insensitive switch set. I already have a sneaky feeling preg_match is faster, but we shall see.

This will be based on the following simple code:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
    
var_dump(preg_match('/^[A-Z\.]+$/i'$myString));

And:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
var_dump(eregi('^[A-Z\.]+$'$myString));

Here are the results:
  • eregi #1: 10.83 seconds
  • eregi #2: 10.41 seconds
  • eregi #3: 10.29 seconds
  • preg_match #1: 10.42 seconds
  • preg_match #2: 10.15 seconds
  • preg_match #2: 10.06 seconds

Surprisingly, not a whole lot of difference. It may be that preg_match has the advantage when complex regular expressions are being used. preg_match is marginally faster than eregi and I'll think I'll be sticking with it as my preference!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-30-2007, 02:06 PM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

Bah! I love sprintf, if only for how readable it makes my code! I guess it's always the same, readability for speed. :(
Karl is offline  
Reply With Quote
Old 09-30-2007, 06:15 PM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
jordie is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Good call! Using your exact same code and looping 1 million times, I pitted eregi against preg_match with the case insensitive switch set. I already have a sneaky feeling preg_match is faster, but we shall see.

This will be based on the following simple code:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
    
var_dump(preg_match('/^[A-Z\.]+$/i'$myString));

And:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
var_dump(eregi('^[A-Z\.]+$'$myString));

Here are the results:
  • eregi #1: 10.83 seconds
  • eregi #2: 10.41 seconds
  • eregi #3: 10.29 seconds
  • preg_match #1: 10.42 seconds
  • preg_match #2: 10.15 seconds
  • preg_match #2: 10.06 seconds

Surprisingly, not a whole lot of difference. It may be that preg_match has the advantage when complex regular expressions are being used. preg_match is marginally faster than eregi and I'll think I'll be sticking with it as my preference!
Thats very interesting. I too thought preg_match would be a lot faster, so i ran your tests as well. To my surprise, I found that I got a second faster for eregi. However, this is running PHP 5.2 on my local machine (so a windows platform and apache server, but its a month old PC and is a fairly decent machine with dual core, so not sure if that comes into play at all).

preg_match: 2.83 seconds
eregi: 1.95 seconds

When I ran the same test on my webserver, I got completely different results. This is PHP 4.4.7 on Linux and the results were a second faster for preg_match. So the complete opposite of my local test.

preg_match: 1.21 seconds
eregi: 2.47 seconds

I also ran my previous speed test on my webserver, but that had no change in the results.

So are the speed of regular expressions dependent on what OS they're on? Or is more to do with which PHP version? I'd like to get more tests on this to find out. Which PHP version & OS did you run your test on?
jordie is offline  
Reply With Quote
Old 10-20-2007, 07:04 PM   #5 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 593
Thanks: 15
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Good call! Using your exact same code and looping 1 million times, I pitted eregi against preg_match with the case insensitive switch set. I already have a sneaky feeling preg_match is faster, but we shall see.

This will be based on the following simple code:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
    
var_dump(preg_match('/^[A-Z\.]+$/i'$myString));

And:

PHP Code:
$myString 'TalkPHP.com';

for(
$i 0$i <= 1000000$i++)
{
var_dump(eregi('^[A-Z\.]+$'$myString));

Here are the results:
  • eregi #1: 10.83 seconds
  • eregi #2: 10.41 seconds
  • eregi #3: 10.29 seconds
  • preg_match #1: 10.42 seconds
  • preg_match #2: 10.15 seconds
  • preg_match #2: 10.06 seconds

Surprisingly, not a whole lot of difference. It may be that preg_match has the advantage when complex regular expressions are being used. preg_match is marginally faster than eregi and I'll think I'll be sticking with it as my preference!
Thats less then not a whole lot, its negligible, here are the average differences beween functions in seconds

#1 4.2 × 10^-7 (.000000042)
#2 1.0 × 10^-8 (.000000001)
#3 2.3 × 10^-7 (.000000023)

That number is so negligible it isnt even worth choosing one over the other.
Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:44 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0