 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
09-29-2007, 04:36 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
|
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. :)
|
|
|
|
09-29-2007, 06:55 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
09-30-2007, 01:06 PM
|
#3 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Bah! I love sprintf, if only for how readable it makes my code! I guess it's always the same, readability for speed. :(
|
|
|
|
09-30-2007, 05:15 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Sep 2007
Location: Sydney, Australia
Posts: 19
Thanks: 0
|
Quote:
Originally Posted by Wildhoney
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?
|
|
|
|
10-20-2007, 06:04 PM
|
#5 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by Wildhoney
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.
|
|
|
|
10-18-2012, 02:32 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
|
|
|
|
10-22-2012, 10:16 AM
|
#7 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
when I go home. Coach Outlet It’s a big part of what I find addictive about living and working in Coach Outlet Store Online this part of the world. You feel like you’re watching the future unfold. Coach Factory Online for the US Open with no success."I prefer to go day-by-day without setting objectives or Coach Factory Online time frames, work hard on my recovery and make sure I keep on getting better little by little."Nadal has been forced Coach Outletto watch from the sidelines as longtime rival Roger Federer returned Coach Purse Outlet Onlineto the top of the world rankings after winning Coach Factory Outlet Onlinea seventh Wimbledon singles title.He has also seen Britain's Andy Murray, Coach Bags Outlet Online who won the men's singles gold medal at the 2012 Olympic Games, move above him in the world rankings after securing his Coach Handbags Outlet first grand slam title at last month's U.S. Open.Currently ranked fourth in the world, Nadal has qualified to play at November's season-ending ATP World Tour Finals in London, Coach Outlet Onlinebut his ability to take part is in doubt due to his injury problems, admitting that his knee is still "bothering him".
|
|
|
|
01-29-2013, 12:36 PM
|
#8 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Organizers said Coach Outlet Online was opportune because the battle’s 150-year anniversary is in December, and Fredericksburg Coach Factory Outlet has been preparing to mark the sesquicentennial. in the new agreement is that Coach Outlet Online revolutionary councils from 14 Syrian provinces now each have a representative, though not all live Coach Online Outlet in Syria. The hope is that will bind the coalition to those inside the country. Perhaps Coach Bags Outlet the most important body the new group is expected to form is a Revolutionary Military Council Coach Factory Online to oversee the splintered fighting organizations and to funnel both lethal and nonlethal Coach Factory Outlet military aid to the rebels. It should unite units of the Free Syrian Army, various militias Coach Outlet Store Online and brigades in each city and large groups of defectors. Before the ink was even dry on the Coach Outlet Store final draft, negotiators hoped that it would bring them the antiaircraft missiles they crave to Coach Factory Stores take on the Syrian Air Force. The United States and Britain have offered only Coach Handbags Outlet nonmilitary aid to the uprising. A similar attempt by the Syrian National Council to Coach Factory Store supervise the military never jelled. Organizers said funding was too haphazard. Eventually foreign Coach Factory Online governments like Qatar and Saudi Arabia, which are financing and arming the rebels, found Coach Factory Online their own favorite factions to deal with. Foreign leaders notably including Secretary of State Coach Outlet Hillary Rodham Clinton urged this unification largely so they could coordinate their Coach Factory Outlet efforts and aid through a group of technocrats. Once it receives international recognition, the Coach Outlet Store Online coalition is supposed to establish a temporary Coach Outlet Online military never jelled.
|
|
|
|
05-14-2013, 12:47 PM
|
#9 (permalink)
|
|
The Wanderer
Join Date: Jan 2013
Posts: 14
Thanks: 0
|
Yes, it’s very interested and I like PHP Codes; Thanks for sharing.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|