Buddha443556 wrote:Jenk wrote:I wonder what the figures would be like for $cnt+=test::one();
I'm betting about the same as procedural.
Not even close.
It is the same as using
with the instance creation outside the loop.
On my system (3.2 gig P4 under XP Pro running XAMPP Server)...
$cnt+=$testclass->one();
1000000 times for 2.9005510807 seconds
Function Procedural
1000000 times for 1.00571393967 seconds
Straight Procedural
1000000 times for 0.375358819962 seconds
$cnt+=test::one();
1000000 times for 1.5131418705 seconds
OOP with class instance outside loop
1000000 times for 1.4020409584 seconds
Code: Select all
<?php
function getmicrotime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$sec+(float)$usec);
}
class test{
function one() {
return 1;
}
}
$t = getmicrotime();
for ($i=0; $i<1000000; $i++){
$testclass=new test();
$cnt+=$testclass->one();
}
$t = getmicrotime() - $t;
print "<h3> 1000000 times for $t seconds</h3>";
$t = getmicrotime();
function one(){
return 1;
}
for ($i=0; $i<1000000; $i++) {
$cnt+=one();
}
$t = getmicrotime() - $t;
print "<h3>1000000 times for $t seconds</h3>";
$t = getmicrotime();
for ($i=0; $i<1000000; $i++){
$cnt+=1;
}
$t = getmicrotime() - $t;
print "<h3>1000000 times for $t seconds</h3>";
$t = getmicrotime();
for ($i=0; $i<1000000; $i++){
$cnt+=test::one();
}
$t = getmicrotime() - $t;
print "<h3>1000000 times for $t seconds</h3>";
$t = getmicrotime();
$testclass=new test();
for ($i=0; $i<1000000; $i++){
$cnt+=$testclass->one();
}
$t = getmicrotime() - $t;
print "<h3> 1000000 times for $t seconds</h3>";
?>
As you can see even with the $testclass=new test(); outside the loop the OOP loop is slower than either of the procedural versions.
OOP is always going to be slower doing the same thing as procedural due to how it functions. There is always more overhead when dealing with OOP code under PHP.
Oops, had to edit for a coding mistake in the $cnt+=test::one(); version as I left the $testclass=new test(); in the loop by mistake.
There is really no difference between
Code: Select all
for ($i=0; $i<1000000; $i++){
$cnt+=test::one();
}
and
Code: Select all
$testclass=new test();
for ($i=0; $i<1000000; $i++){
$cnt+=$testclass->one();
}
other than the first execution of test::one(); will automatically create the object.