- microtime: documentation ( source)
- printf: documentation ( source)
<?php
class EmptySetter {
public $foo = NULL;
public function __set($name, $value) {}
}
class NonEmptySetter {
public function __set($name, $value) {
$this->{$name} = $value;
}
}
function benchmark($class_name) {
$start_time = microtime(TRUE);
for ($i = 0; $i < 1000000; $i++) {
$obj = new $class_name();
$obj->foo = 42;
}
return microtime(TRUE) - $start_time;
}
printf("NonEmptySetter: %.2f seconds\n", benchmark('NonEmptySetter'));
printf("EmptySetter: %.2f seconds\n", benchmark('EmptySetter'));