gorn.ch

Tobias Ebnöther's personal blog

New ReflectionClass versus normal class

In the project I'm currently working on we attend great importance to testing. We looked for a way to replace our data models with mock objects (our current buzz word number one) for easier testing. The mock objects from simpletest are very cool, but we wanted an even simpler solution.

We decided to get our data models by a factory - without touching the existing models - to be able to replace them. A problem was that not all our constructors use the same footprint, which we solved by using ReflectionClass and newInstanceArgs. This is a quite expensive way but makes testing really easy.

Here is the code for my very simple profiling:

<?php
class magicfactory {
function __call($name, $params) {
$class = new ReflectionClass('reflection' . $name);
return $class->newInstanceArgs($params);
}
}
class reflectiontest {
function __construct($foo, $bar) {
}
}
$ref1 = microtime(true);
for ($x=0; $x < 1000000 ;$x++) {
$f = new magicfactory();
$r = $f->test('foo', 'bar');
}
$ref2 = microtime(true);$noref1 = microtime(true);
for ($x=0; $x < 1000000 ;$x++) {
$r = new reflectiontest('foo', 'bar');
}
$noref2 = microtime(true);

And that are the results, I clearly expected that the reflection calls will consume at least double the time since it has to instance two classes:

Reflection time: 16.925868034363s
No reflection time: 4.6512498855591s
The difference is about 0.012274618148804ms each run

Comments (1)  Permalink

Comments

marc @ 22.03.2008 03:34 CET
just a little side note: i guess this is done in api_command.. a simple command takes only 0.01-0.02ms to load/execute. so 0.01ms i quite a lot.. (i assume we have kinda the same processor powers..)
No new comments allowed (anymore) on this post.
Powered by Flux CMS