Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class C1 {
public $x = 0;
public $y = 0;
}
class C2 {
public $x = 0;
public int $y = 0;
}
class C3 {
public int $x = 0;
public int $y = 0;
}
function write_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$a = 2;
$obj->x = $a;
}
}
function assign_op_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$obj->x += 2;
}
}
function pre_inc_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$z = ++$obj->x;
}
}
function post_inc_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$z = $obj->x++;
}
}
function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead,3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
const N = 10000000;
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
write_prop(N, new C1);
$t = end_test($t, 'write untyped', $overhead);
write_prop(N, new C2);
$t = end_test($t, 'write untyped*', $overhead);
write_prop(N, new C3);
$t = end_test($t, 'write typed', $overhead);
assign_op_prop(N, new C1);
$t = end_test($t, '+= untyped', $overhead);
assign_op_prop(N, new C2);
$t = end_test($t, '+= untyped*', $overhead);
assign_op_prop(N, new C3);
$t = end_test($t, '+= typed', $overhead);
pre_inc_prop(N, new C1);
$t = end_test($t, 'pre_inc untyped', $overhead);
pre_inc_prop(N, new C2);
$t = end_test($t, 'pre_inc untyped*', $overhead);
pre_inc_prop(N, new C3);
$t = end_test($t, 'pre_inc typed', $overhead);
post_inc_prop(N, new C1);
$t = end_test($t, 'post_inc untyped', $overhead);
post_inc_prop(N, new C2);
$t = end_test($t, 'post_inc untyped*', $overhead);
post_inc_prop(N, new C3);
$t = end_test($t, 'post_inc typed', $overhead);
total();
<?php
class C1 {
public $x = 0;
public $y = 0;
}
function write_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$a = 2;
$obj->x = $a;
}
}
function assign_op_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$obj->x += 2;
}
}
function pre_inc_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$z = ++$obj->x;
}
}
function post_inc_prop($n, $obj) {
for ($i = 0; $i < $n; ++$i) {
$z = $obj->x++;
}
}
function empty_loop($n) {
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t['sec'] + $t['usec'] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob_end_clean();
$last_time = $end-$start;
$total += $last_time;
$num = number_format($last_time,3);
$pad = str_repeat(" ", 24-strlen($name)-strlen($num));
if (is_null($overhead)) {
echo $name.$pad.$num."\n";
} else {
$num2 = number_format($last_time - $overhead,3);
echo $name.$pad.$num." ".$num2."\n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad."\n";
$num = number_format($total,3);
$pad = str_repeat(" ", 24-strlen("Total")-strlen($num));
echo "Total".$pad.$num."\n";
}
const N = 10000000;
$t0 = $t = start_test();
empty_loop(N);
$t = end_test($t, 'empty_loop');
$overhead = $last_time;
write_prop(N, new C1);
$t = end_test($t, 'write untyped', $overhead);
assign_op_prop(N, new C1);
$t = end_test($t, '+= untyped', $overhead);
pre_inc_prop(N, new C1);
$t = end_test($t, 'pre_inc untyped', $overhead);
post_inc_prop(N, new C1);
$t = end_test($t, 'post_inc untyped', $overhead);
total();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.