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.


Messages In This Thread
Objects: Why are they useful? - by El Forum - 01-22-2008, 02:59 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:18 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:32 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:02 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:20 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:22 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:31 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:38 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:41 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:49 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:52 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:01 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:06 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:19 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 06:55 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:15 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 08:15 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:47 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:59 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 06:34 PM
Objects: Why are they useful? - by El Forum - 01-25-2008, 02:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB