Forum Moderators: coopster
'single quotes' instead of "double quotes" increases performance
That's true, but i'm not sure how significant the difference is. It's because a double quoted string is scanned for variable references (you can put $variable in the middle of a double quoted string), wheras a single quoted string is not and just blatted straight out.
>> Reduce the number of connections to the DB
not always absolute, sometimes a couple queries is better than a single one with multiple joins
'single quotes' instead of "double quotes" increases performanceThat's true, but i'm not sure how significant the difference is.
It's only true if you write sloppy code. If you keep your variable names outside of the quotes (like you should), you will find no significant difference.
So, it's a myth
Anyways, even with sloppy code and the difference that you find, you can hardly talk about 'boosting' the performance. These differences are so minimal they're not really worth the trouble.
well, to contribute something to this topic. a mistake i often see is this:
for ($x=1; $x<count($array); $x++)
the count() will be performed with every loop, thus slowing down the script. So, it's better to store the size of the array in a variable first and use that in the for-loop
don't include every lib on every page, you probably don't need to
don't instantiate huge arrays of data you aren't going to use. If you need to have huge amounts of data around all the time then look into loading it into memory using apache mem hooks
don't make php do things that could be better offloaded to the db or the OS or even to perl or a shell script
Also, instead of i++ use ++i, unless you really need it to be i++.
Is there an actual difference in these two? I thought that they both only existed because of possible errors that could occur from the syntax used(ie ($index++ + $test) should be (++$index + $test))
eelix
eelix
Of course OO has many advantages and you should never loose them out of sight, just for the sake of speed.
Even, though at first glance OO might be a bit slower because of the overhead of objects, it might also allow for much more efficient code, which could make it faster in the long run. Especially in big projects it could be difficult to keep the code efficient if you wrote it completely in a procedural way.
The typical example would be
for($i=0; $i<count($my_array); $i++){
}
This should be rewritten as
$total = count($my_array);
for($i=0; $i<$total; $i++) {
}
'single quotes' instead of "double quotes" increases performance
Yep, I've benchmarked it and many other have. You'll notice that more recnet benchmarks show little or no difference. The underlying code that parses quoted strings and handles variable substitution has been rewritten to make it as efficient as possible in all cases and last I heard there is little or no difference between
$my_word = "obfuscate";
echo "good code should not $my_word";
echo "good code should not " . $my_word;
echo 'good code should not '. $my_word;
These should all be equivalent at this point, though I suppose slightly slower than
echo 'good code should not obfuscate';
Anyway, I posted the benchmarking thread that jatar_k referenced and a few others. I have to say that the big lesson I learned from benchmarking a bunch of stuff is that it is just a waste of time to worry about single quotes versus double quotes or $i++ versus ++$i None of these small changes will add up to a hill of beans. More to the point, if one badly written query on a non-optimized database will use up X, changing every quoted string to the most optimal form will use up 0.0001 X. One "bad" (though possibly necessary) query could take a full second to run and not uncommonly 0.1 seconds for a single query. To get even the small variations I found in the thread referenced, I had to run the test cases tens of thousands of times or using huge strings and running thousands of times.
My top PHP performance tuning tips:
- write organized code that you can understand. Use OOP when it helps toward this goal, divide your script into as many or as few files as necessary to achieve this goal
- don't add features just for features (my corollary to jatar_k's "don't use OOP just to use OOP). Look at most bulletin boards and CMS. What kills them is feature creep. Every time you do a query for something like "Who's online" you eat up lots of resources and, frankly, who really cares?
- put xDebug and CacheGrind on your development platform and if a script is taking a long time to run, profile it and see where the bottleneck is and focus on that bottleneck. Most likely, the bottleneck is due to logic not style. It may be necessary logic as some tasks just take time. That said, if a given function uses up only 0.1% of the execution time used to run the script, what is the point of optimizing that function? At best you get a 0.1% boost at a cost of how many hours?
Cheers,
Tom
eelix
Top 100 Signs That I Am Writing Spaghetti Code in PHP [webmasterworld.com]
for($i = 0; $i < 1000; $i++)
{
echo $i;
}
This method is much slower than something like this:
ob_start();
for($i = 0; $i <1000; $i++)
{
echo $i;
}
ob_end_flush();
Here's my results
1.36509799957 without ob_start()
0.248747825623 with ob_start()
Here's the Script
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
for($i = 0; $i < 100000; $i++)
{
echo ' ';
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo $time.' <br /><br />';
$time_start = microtime_float();
ob_start();
for($i = 0; $i <100000; $i++)
{
echo ' ';
}
ob_end_flush();
$time_end = microtime_float();
$time = $time_end - $time_start;
echo $time.' <br /><br />';
?>
Also, is it necessary to have a redundant ob_end_flush() at the end of a script?
Using php cache is another performance boost, but I still don't know how to use it.