Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Last active May 23, 2016 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/778f881ff5e66b9177c9a8396f7b6ede to your computer and use it in GitHub Desktop.
Save krakjoe/778f881ff5e66b9177c9a8396f7b6ede to your computer and use it in GitHub Desktop.
krakjoe@fiji:/usr/src/php-src$ sapi/cli/php -n prop.php
empty_loop 0.060
write_prop1() 0.085 0.025
write_prop2() 0.070 0.010
write_prop3() 0.058 -0.002
------------------------
Total 0.274
krakjoe@fiji:/usr/src/php-src$ sapi/cli/php -n prop.php
empty_loop 0.059
write_prop1() 0.086 0.027
write_prop2() 0.070 0.011
write_prop3() 0.059 -0.000
------------------------
Total 0.274
krakjoe@fiji:/usr/src/php-src$ sapi/cli/php -n prop.php
empty_loop 0.058
write_prop1() 0.085 0.026
write_prop2() 0.071 0.012
write_prop3() 0.059 0.000
------------------------
Total 0.272
<?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) {
$obj->x = 0;
}
}
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 = 5000000;
class_exists(C1::class);
class_exists(C2::class);
class_exists(C3::class);
$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_prop1()', $overhead);
write_prop(N, new C2);
$t = end_test($t, 'write_prop2()', $overhead);
write_prop(N, new C3);
$t = end_test($t, 'write_prop3()', $overhead);
total();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment