Re: [PHP3] Re: [PHP-DEV] Re: [PHP3] PHP Speed Optimization
| From: | Ron Chmara | Date: | Mon, 24 Apr 2000 23:08:25 +0000 |
| Subject: | Re: [PHP3] Re: [PHP-DEV] Re: [PHP3] PHP Speed Optimization | ||
| References: | 1 2 3 4 5 6 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-18166@lists.php.net to get a copy of this message | ||
Faisal Nasim wrote:
> Seems like you have mixed C with PHP. PHP functions don't
> have overhead like C does (unless they are inline, the overhead
> is negligible, but its there!)
Hence, my emphasis in this thread between user generated functions,
and PHP internal functions.... they are not the same, but user functions
can (and do) suck up CPU and RAM when loading and parsing. Here, make a web page
with 2Mb of unused functions in the page, and at the bottom:
echo "Hello World";
Let me know if it's as fast as only one line, no excess function loading,
echo "Hello World";
> | If that's still not fast enough, look into coding directly in
> | C.
> You will choose PHP when you don't need C or you need rapid
> development with easy interchangeable database connectivity
> and blah blah.
Right. Different tools for different jobs. If you need maximum execution
speed, avoid PHP and use C. If you want rapid development and don't need
blistering speed, PHP is great.
> | Now, of course, this all makes for faster execution, but your
> | code can bloat like mad, or become almost unreadable. If the goal,
> | however, is speed, that is a sacrifice you may *have* to make...
> This might look like a silly example, but it demonstrates the goal.
> I don't use PHP3, sorry. This is done with PHP4 (CVS, not latest).
Well then, for starters, perhaps you have not noticed my point
about speeds in PHP3 vs. PHP4, and that speed coding isn't a
one-size-fits-all metality. :-)
No functions, PHP 4, Win2000, custom code:
> Three runs: All report 36 seconds.
With functions, PHP 4, Win2000, custom code:
> Three runs: All report 22 seconds.
My speeds on PHP3.0.14:
Well, for starters, your functions won't run in PHP3, as you stuck them at the
end of the file (and your have to delcare them in the beginning). So, I moved
them up, and got:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to
allocate 19 bytes) in /www/functiontest2.php3 on line 3
Well, gee. What's the speed of dark? So:
I ran a 1/10 of the iterations (Dual 500Mhz, 512Mb of RAM, live
ldap/mail/web/db server averaging 300-500 active users) on the PHP3
code, 14/14/14 seconds, for PHP3.
To run the second test, I had to move your functions to
the top, and got 10/10/10 for the tests. In this code block, with a
tenth of the iterations, in PHP3, functions would be the way to go. With
half the iterations, I had 53/18/25/28 seconds *with* functions, 25/23/19
seconds without. Why the funky numbers? The server was getting
bogged, with 66MB of http processes alone.
> On Windows 2000, 192 (128+64)mb of ram with all other programs shutdown
> for testing.....
> What do you say now?
I'd say that in PHP, on a large code blocks with many iterations, and enough
RAM to hold the entire code block, you have demonstrated a case where functions
*are* faster. What is faster in one case is _still_ not the fastest in all
cases. I can write hundreds of poorly coded functions that would run horribly
slow, or fast non-user-function code, just as you could write clean functions
that ran better than explicitly invoked code.
Here's an example of how functions are resource draining:
(code from "Core PHP programming"):
<?
$start = time();
for($index = 0; $index < 1000000; $index++)
{
$value += (cos(time()%360));
}
$finish = time();
print("Elapsed Time: " .
($finish - $start) .
" seconds<BR>\n");
?>
And this
<?
function calculate($period)
{
return(cos(time()%$period));
}
$start = time();
for($index = 0; $index < 1000000; $index++)
{
$value += calculate(360);
}
$finish = time();
print("Elapsed Time: " .
($finish - $start) .
" seconds<BR>\n");
?>
Running without a function, I consistantly get %50-80 of the speed I get
_with_ a function.... and thats 20%-100% faster by _not_ using functions....
So, now we know that functions are faster, and slower? How fast does the
abpove function code run on your PHP4 box?
-Bop-Bop
--
Brought to you from boop!, the dual boot Linux/Win95 Compaq Presario 1625
laptop, currently running RedHat 6.1. Your bopping may vary.
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 19 bytes) in
/www/phptest/functiontest2.php3 on line 3