Jimmy Ruska's Blog
Php TutorialsPHP Get Character Frequency
Cache Slow Pages for Performance
Youtube subscription Checker
Resizing images in PHP
Zip a File with PHP
Getting next auto_increment value from Mysql
Install PHP, MySQL, Apache the Bullet Proof Way
Benchmark your PHP with 3 lines of code
See all 8 posts
Php Tutorials RSS

Categoriesvideo tutorials (123)
funny pictures (41)
amazing videos (27)
hilarious videos (26)
amazing pictures (21)
misc (10)
php tutorials (8)
computer tricks (7)
personal updates (6)
cat videos (5)
articles (3)
youtube tips (3)
apache (2)
fake news (2)
My WebsitesBest of Internet
Streaming Anime Episodes
Free Video Tutorials
Best of Youtube
Electronics Discounts
MP3s From Google
Free Online Education
Funny Pictures Blog
Video Game Sheet Music
Free Movies Online
Online Degrees
College Online
Feb 12 | Benchmark your PHP with 3 lines of code Posted on Tuesday, February 12 2008 |
You already know there's multiple ways of working out the same problem, but how do you know which way is the most efficient? It's not like math where you can pick the path with the least amount of writing. The goal is still to accomplish the task in the least amount of steps, but different functions have varying amounts of underlying code.
Simple Example
Let's say you're doing a simple code that removes the word 'foo' and 'bar' from a string. Here are some possible ways to continue...
Possibility 1$string=ereg_replace('foo|bar','',$string);
$string=preg_replace('/foo|bar/','',$string);
$kill = array('foo','bar');
$string = str_replace($kill, '', $string);
All of the above examples take an insignificant amount of time to complete, but in real world tasks efficiency is important. For instance, lets say you're removing the most common words from huge files to collect statistics, or you're the cult of scientology and you have 10,000 banned keywords users can't post in your forum.
Testing for Speed with 3 lines
To find the speed we need to start and end a timer, then echo the total time once completed. The following lines accomplish this.
$end=microtime(true); // end timer
echo $totaltime=$end-$start; // echo results
Lets apply it to the first example above...
$string='I am a string. bar foo';
$string=ereg_replace('foo|bar','',$string);
$end=microtime(true);
echo $totaltime=$end-$start;
It's much more comprehensible to the average person to see the differences in terms of seconds rather than microseconds. Let's say that one example takes 1 millisecond and another takes 5 milliseconds. What's a few milliseconds anyway? If you ever have code that runs for hours on end you'll understand 5 times faster is 5 times faster. Setting up a long loop can help make differences evident.
for ($i=0;$i<10000;$i++){
$string=ereg_replace('foo|bar','',$string);
}
Note
Don't echo anything or add any other irrelevant code between the timers. Doing so will skew the results.
Exercises
- Opening Links: fopen vs fsockopen vs file_get_contents vs Curl
- Using double quotes vs single quotes for strings that have tons of variables inside.
- loops: foreach vs switch/case vs for vs while/do
Tags: php, speed test, benchmark, scientology, efficiency, php tricks

Share:
More OMFG-Good Links
See all Posts in the Funny Pictures category.Download mp3s faster than limewire using google.
I've made 100+ free video tutorials.
See the best of the internet today on one page.