How efficient is OOP in PHP>>

In this article, we are going to investigate the efficiency of Object Oriented Programming (OOP) in PHP, and by efficiency, we mean execution time.

We are going to start by running some test cases. These test cases are going to be kept simple, since the main point of this "experiment" is to truly get the gist of how much overhead there really is when using classes. Obviously, this is not a true object oriented program, but the use of classes contribute quite a bit, and is just enough to get an idea of how efficient OOP is in PHP.

Here is the code we are going to use for our first test case:
OOP Function Neither
class test
{
 

    function one() {
     

      return 1;
       

    }
     

}

for ($i=0; $i<1000000; $i++)
{
 

    $testclass=new test();
    $cnt+=$testclass->one();
     

}
 

function one()
{
 

    return 1;
     

}

for ($i=0; $i<1000000; $i++)
{
 

    $cnt+=one();
     

}
 

for ($i=0; $i<1000000; $i++)
{
 

    $cnt+=1;
     

}
 



Here are the execution times for the code given above. We ran through each snippet of code 10 times to get a more accurate result.

First test case (in seconds)

# OOP Function Neither
1 5.025 1.748 1.029
2 5.044 1.752 1.034
3 4.996 1.732 1.051
4 4.978 1.737 1.008
5 5.000 1.757 1.045
6 5.038 1.726 1.034
7 5.100 1.745 1.053
8 5.091 1.744 1.026
9 5.021 1.748 1.035
10 4.986 1.755 1.041
Avg 5.028 1.744 1.036

* These tests were run on a Dual Xeon 2.0Ghz (using hyper-threading) with 2GB RAM.

As we can see, using just functions are approximately 288% faster, and using neither functions nor classes are 486% faster in this test case. From this test case, we can see that using classes for simple tasks end up being very inefficient.

Here is the code we are going to use for our second test case:

OOP Function Neither
class test
{
 

    function one()
    {
     

      return 1;
       

    }
     

}

for ($i=0; $i<100000; $i++)
{
 

    $testclass=new test();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
    $cnt+=$testclass->one();
     

}

function one()
{
 

    return 1;
     

}

for ($i=0; $i<100000; $i++)
{

    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
    $cnt+=one();
     

}
 

for ($i=0; $i<100000; $i++)
{
 

    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
    $cnt+=1;
     

}


Basically, the main difference between the first test case code and the second test case is that we are simulating 10 functions being executed at once instead of just one. This may decrease the time for the OOP results due to the fact that the class only has to be initialized once for every 10 function calls, instead of on every function call.

Here are the execution times for the second test case, once again, we ran through each snippet 10 times for more accurate results.

Second test case (in seconds)
 

# OOP Function Neither
1 2.389 1.154 0.366
2 2.362 1.118 0.377
3 2.361 1.172 0.373
4 2.392 1.127 0.377
5 2.348 1.210 0.375
6 2.412 1.101 0.381
7 2.369 1.160 0.370
8 2.362 1.109 0.376
9 2.351 1.164 0.375
10 2.392 1.113 0.372
Avg 2.374 1.143 0.374

* These tests were run on a Dual Xeon 2.0Ghz (using hyper-threading) with 2GB RAM.

In the second test case, we see OOP being more efficient, and the difference between OOP and functions is not as great as in the first test case. In this test case, functions are approximately 208% faster, and using neither functions nor classes are 635% faster.

From our two quick test cases, we see that using OOP in PHP can be considerably slower. But just because it is slower, does not mean we should stop writing OOP code all together, but instead, we should consider the function approach for pages that tend to get a large number of hits per day. It is also possible to use neither, but really, do we want to maintain code that does not use functions at all?

Therefore, the next time you are developing PHP code, you should consider whether you want faster execution times / less CPU load, or easier to maintain code.


<< Back


Google






   



MSN Nick Name



More Resources...





Most Viewed Services:
  1. HTML Tutorial
  2. XHTML Tutorial
  3. CSS Tutorial
  4. Javascript Tutorial
  5. DHTML Tutorial
  6. VB Script
  7. TCP/IP Tutorial
  8. ADO Tutorial
  9. MYSQL Tutorial
  10. ASP Tutorial
  11. AJAX Tutorial
  12. CFML Tutorial
  13. PHP Tutorial
  14. WML Tutorial
  15. FLASH Tutorial
  16. XML Tutorial
  17. RSS Tutorial
  18. SQL Tutorial
  19. HTML Articles
  1. Javascript Articles
  2. PHP Articles
  3. SEO Articles
  4. Web Design Articles
  5. SEO Tips
  6. Web Design Tips
  7. Articles
  8. CSS
  9. CSS Tips
  10. HTML Tips
  11. JAVASCRIPT Tips
  12. MYSQL Tips
  13. PHP Tips
  14. Money
  15. Tutorials
  16. Web Hosting



  • Home
  • Web Directory
  • Top Directoriers
  • Webmaster Directories
  • Contact
  • © Copyright 2006 All Rights Reserved By CodeDcode.Com