Jump to content

Faster way to echo {$array['foo']} or ".$array['foo']." or $foo = $array['foo']


Recommended Posts

I am using echo to display a table with data from mysql database. Since there is quite a bit of data I want it to display as fast as possible. What is the faster way to echo elements of the row i get from mysql query result?

 

Right now i am doing  echo "blah {$row['foo']} blah";

before i had echo "blah".$row['foo']."blah"; but using the . seems slow

the other options are:

1: $element = $row['foo'];

echo "blah $element blah";

2: Use a stringBuilder library found here: http://cfgmgr.mirrors.phpclasses.org/browse/file/23722.html

it uses sprintf to concat strings on lower level

 

All suggestions are appreciated, Thank you in advance

Luco

 

I cannot believe that the concatonation operator (.) is any different.

It is the amount of data you are querying and looping through that will make things slow. If it is for on-screen display then why not paginate the results?

Benchmarks using loops of 10,000 echo statements have shown that putting a variable inside of a double-quoted string is slightly faster than using concatenation. If my memory is correct, a few 1/100 of a second for a loop of 10K.

 

You would need to benchmark if that or a sprintf() is faster.

 

Assigning one variable to another just to put the second variable into a string, when the first variable could be put into the string directly, would always be less efficient than just putting the original variable into the string.

The differences in time spent using any of those methods will be negligible when compared to the time spent by your script talking to MySQL and likely other things happening in your script. Don't worry about which way of echoing a value is quicker until you really need it. Even then, do your own case-specific thorough research to arrive at a conclusion (if you can).

Correct me if I'm wrong, but using a loop would mean the processor would be calculating the same logic over and over again (unless of course the variable used was created dynamically inside the loop?? which would cause in-accurate results...)

Wouldn't that cause an increase in processing speed after the first iteration due to the cache controller pulling the calculated data and result into L1 Cache and causing in-accurate results for the remaining iterations?

Quick benchmark (on my PC)

echo "test $r";
//0.58999300003052

echo 'test '.$r;
//0.60724496841431

echo "test {$r}";
//0.65190696716309

sprintf( "test %s",$r);
//0.60445594787598

sprintf( 'test %s',$r);
//0.52453398704529

 

 

EDIT:

sample code

<?php
$rnd = array();
for($n=0;$n<1000;$n++){
$rnd['test'.$n] = md5(uniqid(true));
}
$start = microtime(true);
foreach($rnd as $K => $r){
echo "test $r";
}
echo "<BR />\n";
echo microtime(true)-$start;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.