IT Community - Software Programming, Web Development and Technical Support

Tricky coding in php

This is a discussion on Tricky coding in php within the PHP Programming forums, part of the Web Development category; Use single quoted strings unless you need to interpolate variables into your string. This saves php the time to scan ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-18-2007, 01:22 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Tricky coding in php

Use single quoted strings unless you need to interpolate variables into your string. This saves php the time to scan the string for contained variables and saves about 50% execution time.

data
double quotes: 0.001505970954895
single quotes: 0.00078308582305908
difference...: 0.00072288513183594

benchmark
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
#
$start = microtime();
$a = array();
for($i=0;$i<100;$i++) {
array_push($a, "Aaron rules!");
}
$end = microtime();
$t1 = (getmicrotime($end) - getmicrotime($start));
echo "<pre>double quotes: $t1<br>";
#
unset($a);
#
$start = microtime();
$b = array();
for($i=0;$i<100;$i++) {
array_push($b, 'Aaron rules!');
}
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
$t3 = $t1-$t2;
echo "single quotes: $t2<br>difference...: $t3</pre>";
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2007, 01:24 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: Tricky coding in php

Use concatenation rather than sprintf(). Use sprintf rather than interpolation.
Use sprintf to build more complex strings such as SQL query strings.

data
concatenation: 0.0008620023727417
sprintf......: 0.0017820596694946
interpolate..: 0.002047061920166

benchmark
$x = 'Aaron';
$y = 'C.';
$start = microtime();
for($i=0;$i<100;$i++) {
$a = $x . $y . ' rules!<br/>';
}
$end = microtime();
$t1 = (getmicrotime($end) - getmicrotime($start));
echo "<pre>concatenation: $t1<br>";
#
unset($a);
#
$start = microtime();
for($i=0;$i<100;$i++) {
$a = sprintf('%s %s rules!<br/>', $x, $y);
}
$end = microtime();
$t1 = (getmicrotime($end) - getmicrotime($start));
echo "<pre>sprintf......: $t1<br>";
#
unset($a);
#
$start = microtime();
for($i=0;$i<100;$i++) {
$a = "$x $y rules!<br/>";
}
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
$t3 = $t1-$t2;
echo "interpolate..: $t2<br>difference...: $t3</pre>";
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-19-2007, 03:33 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: Tricky coding in php

Use push/join rather than concatenation.

data
using concat: 0.00052297115325928 seconds
using array.: 0.00033998489379883 seconds
difference..: 0.00018298625946045 seconds
benchmark
$x = array('Taylor','Zac','Isaac','Leo','Aaron','Nick', 'Scott',
'Dave','Bob','Clint','Chris','Thomas','Ryan','Denn is',
'Thimo','Steve');
#
$s1 = microtime();
foreach($x as $val) {
$s .= $val . ', ';
}
$e1 = microtime();
#
unset($s);
$s = array();
#
$s2 = microtime();
foreach($x as $val) {
array_push($s, $val);
}
$y = implode(', ', $s);
$e2 = microtime();
#
$t1 = (getmicrotime($e1) - getmicrotime($s1));
$t2 = (getmicrotime($e2) - getmicrotime($s2));
$td = $t1 - $t2;
#
echo "<pre>using concat: $t1 seconds<br>
using array.: $t2 seconds<br><br>
difference..: $td seconds</pre>";
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-19-2007, 03:34 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: Tricky coding in php

For outputting text with variables, use the little-known fact that echo allows several arguments, instead of concatenation or double quotes (which are twice as slow!):

echo 'text', $var, 'more text', $anothervar;

data:
commas.......: 0.0024470090866089
concatenation: 0.0028690099716187
double quotes: 0.0044499635696411

script:
<?
$x = 'H.'; $y = 'J.'; $z = 'O.';
$start = microtime();
for($i=0;$i<100;$i++) { echo $x, ' ', $y, ' ', $z, ' rules! '; }
$end = microtime();
$t1 = (getmicrotime($end) - getmicrotime($start));
unset($a);
#
$start = microtime();
for($i=0;$i<100;$i++) { echo $x.' '.$y.' '.$z.' rules! '; }
$end = microtime();
$t2 = (getmicrotime($end) - getmicrotime($start));
unset($a);
#
$start = microtime();
for($i=0;$i<100;$i++) { echo "$x $y $z rules! "; }
$end = microtime();
$t3 = (getmicrotime($end) - getmicrotime($start));
unset($a);
#
echo "<pre>commas.......: $t1<br></pre>";
echo "<pre>concatenation: $t2<br></pre>";
echo "<pre>double quotes: $t3</pre>";
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
?>
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-19-2007, 03:36 AM
vijayanand vijayanand is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 293
vijayanand is on a distinguished road
Default Re: Tricky coding in php

Use PHP´s feature to easily switch between html and php mode whenever you want/need to. This comes at very little cost. When you need to output just a single variable use <?=$var?> instead of <?echo $var?>.
The printf() approach was faster once more than 8 variables were used to build the option elements.

data
html/php mode (<?=)...: 0.2423449754715
html/php mode (<?echo): 0.25177192687988
printf()..............: 0.27242302894592
__________________

J.Vijayanand
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/2097-tricky-coding-php.html
Posted By For Type Date
hosting php web - Tricky coding in php This thread Refback 07-18-2007 08:50 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
ColdFusion does not require coding in traditional programming languages senthilkannan ColdFusion Programming 1 09-29-2008 12:25 AM
Free templates or coding on your own? capture Web Design Help 12 09-26-2008 01:18 AM
Coding Standared saravanan PHP Programming 61 04-11-2008 02:10 AM
C- Tricky Question & answers With explanation vijayanand C and C++ Programming 46 01-03-2008 11:00 PM


All times are GMT -7. The time now is 07:01 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0