Welcome Guest, Not a member yet? Register   Sign In
Objects: Why are they useful?
#21

[eluser]tonanbarbarian[/eluser]
Ok i decided to do some benchmarking and i was very surprised. (Yes I am always doing these little benchmarking exercises)

here is the code i ran
Code:
function test() {
        $loops = 100000;

        $this->benchmark->mark('object_aaa_start');
        for ($i=0; $i<$loops; $i++) {
            $obj = new aaa();
            $obj->foo = 123;
            $obj->bar = 123;
        }
        $this->benchmark->mark('object_aaa_end');

        $this->benchmark->mark('object_bbb_start');
        for ($i=0; $i<$loops; $i++) {
            $obj = new bbb();
            $obj->foo = 123;
            $obj->bar = 123;
        }
        $this->benchmark->mark('object_bbb_end');

        $this->benchmark->mark('object_stdClass_start');
        for ($i=0; $i<$loops; $i++) {
            $obj = new stdClass();
            $obj->foo = 123;
            $obj->bar = 123;
        }
        $this->benchmark->mark('object_stdClass_end');

        $this->benchmark->mark('array_start');
        for ($i=0; $i<$loops; $i++) {
            $obj = array();
            $obj['foo'] = 123;
            $obj['bar'] = 123;
        }
        $this->benchmark->mark('array_end');

        $this->benchmark->mark('array_object_start');
        for ($i=0; $i<$loops; $i++) {
            $obj = array();
            $obj['foo'] = 123;
            $obj['bar'] = 123;
            $obj = (object) $obj;
        }
        $this->benchmark->mark('array_object_end');

        echo 'Done';
    } // test
class aaa {
    var $foo = 0;
    var $bar = 0;
}

class bbb {
    var $foo = 0;
}

aaa is running a class with 2 predeclared properties
bbb is a class with just 1 predeclared property and the other must be declared at run time
stdClass is creating an empty class and assigning both properties at run time
Array is creating an empty array and assigning elements
Array Object creates the array like Array but then casts to object

I run the tests 100,000 times each and record the time. I then got the average of 5 full runs
The first results are PHP 4.4.8 CGI mode in Apache
aaa: 0.1635
bbb: 0.1615
stdClass: 0.1680
Array: 0.1295
Array Object: 0.1980

The second results are PHP 5.2.3 API mode in Apache
aaa: 0.1309
bbb: 0.1233
stdClass: 0.1205
Array: 0.0776
Array Object: 0.1397

So surprisingly what this show is that assigning properties to existing classes at run time is quicker somehow.
And in PHP 5 stdClass is quicker yet again.
Overall using arrays is significantly quicker than objects just to handle data
And if you are using PHP 5 then casting is not too bad if you must use an object.

Draw what ever conclusions you want from this... but I will stick with using arrays as the results from the database, and as storage mechanisms for any data I use in views.
#22

[eluser]wiredesignz[/eluser]
I can't see how 4/100th's of a second is significantly faster over 100,000 iterations. Even if I mis-read and its for 1 iteration it's really negligable.

I'll stick with objects.
#23

[eluser]ejangi[/eluser]
It isn't for one page request, but for a box running multiple sites with many many pages requests per second it does make a difference.
#24

[eluser]wiredesignz[/eluser]
Isn't that what Output caching is all about?

I really don't think any script, procedural or OOP is going to perform well with 1000's of page requests per second.

I read somewhere that a simple CI 'hello world' type application was only good for about 50 page requests per second anyway. (which is better than some other frameworks I won't mention)
#25

[eluser]Edemilson Lima[/eluser]
Quote:I read somewhere that a simple CI ‘hello world’ type application was only good for about 50 page requests per second anyway.

It depends on your web server resources. If you need that much requests, may you need a dedicated server. But in any case, you a well tuned application is essential.




Theme © iAndrew 2016 - Forum software by © MyBB