The Server Pages
:: TSP Menu
- Home
- Authors
- Glossary
- RSS Feed

:: Servers
- CPanel
- Hosting
- Linux
- Windows

:: Webmaster
- Databases
- Design
- PHP
- Search Engines

:: Scripts
- Scripts

:: Documentation
- PHP Manual

:: Other
- Errors & Solutions
- TechPunt
- Wojjie

Google
Web
TSP (site)

»

TheServerPages Articles

»

Webmasters

»

PHP

»

Performance


Performance of OOP PHP versus non-OOP

Author: Wojjie     Posted: (2004-05-13)     Viewed: (10671 times)


Introduction:

There has been a debate among various groups wether it is better to use Object Oriented Programming (OOP) for developing web applications or not. It is possible to develop applications that perform the same tasks wether you decide to use OOP or not, so what does it come down to? In this article we will be making a comparision between the two in terms of performance/speed. We will not be making any conclusions here as to which is better, since that would require a more lengthy comparision.

I have previously written an article on this matter, but the test I used was controversial.


Server Specs:

Dual Xeon 2.8ghz, 2GB ECC RAM
PHP 4.3.4
Zend Engine v1.3.0
  with Zend Extension Manager v1.0.1
  with Zend Optimizer v2.5.0


PHP OOP Test Script (Script A)
function getmicrotime() //grabbed from PHP.net documentation
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}


class basket {
        var $apples;

        function basket($apples) {
                $this->apples=$apples;
        }

        function addApple() {
                $this->apples++;
        }
}

$b1=new basket(10); // start with 10 apples

$t1=getmicrotime(); // start stopwatch
for ($i=0; $i<50000; $i++) {
        $b1->addApple();
        $b1->addApple();
}
$t2=getmicrotime(); //stop stopwatch

echo ($t2-$t1); //result time

PHP non-OOP Test Script (Script B)
function getmicrotime() //grabbed from PHP.net documentation
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

function addApple($basket) {
        return $basket++;
}

$b1=10; // start with 10 apples

$t1=getmicrotime();
for ($i=0; $i<50000; $i++) {
        $b1=addApple($b1);
        $b1=addApple($b1);
}
$t2=getmicrotime();

echo ($t2-$t1); //result time

Explaination:

In the two tests, we are testing the execution time of functions and class functions. In an attempt to make both tests equal, I programmed both test scripts in a manner that can be used for the same applications. Both scripts can create 'baskets' and increment the number of apples in each of those baskets. To make things simple, we will only be testing the execution time of one function, incrementing the number of apples.

Since times may vary from test to test, I have decided to run this test multiple times, and also alternating the test scripts. (ie: A B A B ...)

Results

Finally, here are the results in a table format:

OOPNon-OOP
0.3065049650.252082109
0.3298621180.285518885
0.3234250550.295748949
0.3043088910.319816113
0.3247430320.251178026

OOP Average:0.31777
Non-OOP Average:0.28087

Therefore OOP comes out to be 13% slower than Non-OOP.



Comments

Links: Game-Monitor
Copyright © 2004-2011: TheServerPages.com