Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Thursday, March 1, 2007

Textout with PHP, which one is really the fastest?

(This is a bit off-topic from Agent55). The other day I saw a discussion on a forum about what php-code to use if you want your script to be as FAST as possible. The discussion was about whether "print" or "echo" should be used, and I thought that probably the <php ?> tags would be faster to use than either of print and echo.

Which is faster? (With faster I mean the one that executes in less time)

Hi <?=$username?> !!!

or
<?echo "Hi $username !!!"?>

Ofcourse I had to make an empirical test, to know for sure what method was the fastest, So I hacked up these two (in functionality) identical code-snippets.

The "echo-method"

<?$username="test";$time_start = microtime(true);
for($i=0;$i<1000000;$i++){?><?echo "Hi $i $username !!!"?><?}
$time_end = microtime(true);
?><?=($time_end - $time_start)?>


The "inline-method"
<?$username="test";$time_start = microtime(true);
for($i=0;$i<1000000;$i++){?>Hi <?=$i?> <?=$username?> !!!<?}
$time_end = microtime(true);
?><?=($time_end - $time_start)?>


The results
I have tested these two php-snittets in two single php-files on my test-server and the results are conclusive. Not ONCE did the "echo-method" prove to be faster than the inline-method. In my particular test-case, the "inline-method" was around 0.5 seconds faster than the "echo-method"

Conclusion
So what is the conclusion of this then? Well, in a normal php-development case you won't have to care about these two differences because in my test-code I did 1 miljon repeats of the code to get a difference of only half a second between the two, so this is only a test of the principle.

What I do know is that the "inline-method" is a much more appreciated way to create dynamic content, at least by almost any web-designer I have talked to about this.
So now when it proved to be slightly faster than the "echo-method", I see no reason to use the "echo-method" any longer generally speaking, there will always be exceptions to this ofcourse.

What do you think? Feel free to use the code-snippets above to test them on your own server.


Digg!