Skip to content

Instantly share code, notes, and snippets.

@hikari-no-yume
Last active November 5, 2016 18:53
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 hikari-no-yume/89eda3aa5711ca3fd2f7a526bffaa37c to your computer and use it in GitHub Desktop.
Save hikari-no-yume/89eda3aa5711ca3fd2f7a526bffaa37c to your computer and use it in GitHub Desktop.
object/array cast fix benchmark results
<?php
echo basename(PHP_BINARY), " - array/object benchmark", PHP_EOL;
echo "---", PHP_EOL;
const ITERATIONS = 100000000;
function benchmark(string $title, \Closure $thing) {
echo $title, ":", PHP_EOL;
$start = microtime(true);
$thing();
$end = microtime(true);
echo "* ", ($end - $start), "s", PHP_EOL;
}
$arr = ["a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "f" => 6];
benchmark("Array with string keys to object cast, " . ITERATIONS . " iterations", function () use ($arr) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (object)$arr;
}
});
$arr = [1, 2, 3, 4, 5, 6];
benchmark("Array with integer keys to object cast, " . ITERATIONS . " iterations", function () use ($arr) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (object)$arr;
}
});
$arr = ["a" => 1, "b" => 2, "c" => 3, 4, 5, 6];
benchmark("Array with string and integer keys to object cast, " . ITERATIONS . " iterations", function () use ($arr) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (object)$arr;
}
});
$obj = new StdClass;
$obj->a = 1;
$obj->b = 2;
$obj->c = 3;
$obj->d = 4;
$obj->e = 5;
$obj->f = 6;
benchmark("stdClass with non-numeric keys to array cast, " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (array)$obj;
}
});
benchmark("stdClass with non-numeric keys get_object_vars(), " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = get_object_vars($obj);
}
});
$obj = new StdClass;
$obj->{'0'} = 1;
$obj->{'1'} = 2;
$obj->{'2'} = 3;
$obj->{'3'} = 4;
$obj->{'4'} = 5;
$obj->{'5'} = 6;
benchmark("stdClass with numeric keys to array cast, " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (array)$obj;
}
});
benchmark("stdClass with numeric keys get_object_vars(), " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = get_object_vars($obj);
}
});
$obj = new StdClass;
$obj->a = 1;
$obj->b = 2;
$obj->c = 3;
$obj->{'0'} = 4;
$obj->{'1'} = 5;
$obj->{'2'} = 6;
benchmark("stdClass with numeric and non-numeric keys to array cast, " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (array)$obj;
}
});
benchmark("stdClass with numeric and non-numeric keys get_object_vars(), " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = get_object_vars($obj);
}
});
$obj = new class {
public $a = 1;
public $b = 2;
public $c = 3;
public $d = 4;
public $e = 5;
public $f = 6;
};
benchmark("Custom class with non-numeric declared properties to array cast, " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = (array)$obj;
}
});
benchmark("Custom class with non-numeric declared properties get_object_vars(), " . ITERATIONS . " iterations", function () use ($obj) {
for ($i = 0; $i < ITERATIONS; $i++) {
$foo = get_object_vars($obj);
}
});
echo "---", PHP_EOL;

macOS Sierra, Version 10.12

1.3 GHz (i5-4250U) dual-core Intel Core i5 with 3 MB shared L3 cache

Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Configure line: YACC=/usr/local/opt/bison27/bin/bison ./configure --enable-phpdbg --disable-all --disable-cgi --enable-opcache


php-master-2016-11-05-74ce3ed - array/object benchmark

Array with string keys to object cast, 100000000 iterations:
* 9.3665030002594s
Array with integer keys to object cast, 100000000 iterations:
* 9.3693268299103s
Array with string and integer keys to object cast, 100000000 iterations:
* 9.3166899681091s
stdClass with non-numeric keys to array cast, 100000000 iterations:
* 4.6070840358734s
stdClass with non-numeric keys get_object_vars(), 100000000 iterations:
* 5.6828849315643s
stdClass with numeric keys to array cast, 100000000 iterations:
* 4.544949054718s
stdClass with numeric keys get_object_vars(), 100000000 iterations:
* 5.6619122028351s
stdClass with numeric and non-numeric keys to array cast, 100000000 iterations:
* 4.6021101474762s
stdClass with numeric and non-numeric keys get_object_vars(), 100000000 iterations:
* 5.6313982009888s
Custom class with non-numeric declared properties to array cast, 100000000 iterations:
* 10.43790102005s
Custom class with non-numeric declared properties get_object_vars(), 100000000 iterations:
* 28.866168022156s

php-fixObjectArrayCastsZippy-2016-11-05-8f67845 - array/object benchmark

Array with string keys to object cast, 100000000 iterations:
* 11.01106095314s
Array with integer keys to object cast, 100000000 iterations:
* 37.67609000206s
Array with string and integer keys to object cast, 100000000 iterations:
* 30.588332891464s
stdClass with non-numeric keys to array cast, 100000000 iterations:
* 5.4515690803528s
stdClass with non-numeric keys get_object_vars(), 100000000 iterations:
* 6.5059370994568s
stdClass with numeric keys to array cast, 100000000 iterations:
* 16.314330101013s
stdClass with numeric keys get_object_vars(), 100000000 iterations:
* 17.18993806839s
stdClass with numeric and non-numeric keys to array cast, 100000000 iterations:
* 16.893738031387s
stdClass with numeric and non-numeric keys get_object_vars(), 100000000 iterations:
* 17.094678878784s
Custom class with non-numeric declared properties to array cast, 100000000 iterations:
* 10.945954084396s
Custom class with non-numeric declared properties get_object_vars(), 100000000 iterations:
* 27.078000068665s

php-fixObjectArrayCastsZippyDmitry-2016-11-05-47a08f8 - array/object benchmark

Array with string keys to object cast, 100000000 iterations:
* 10.461206912994s
Array with integer keys to object cast, 100000000 iterations:
* 37.209656953812s
Array with string and integer keys to object cast, 100000000 iterations:
* 29.648227930069s
stdClass with non-numeric keys to array cast, 100000000 iterations:
* 5.6267850399017s
stdClass with non-numeric keys get_object_vars(), 100000000 iterations:
* 6.5360980033875s
stdClass with numeric keys to array cast, 100000000 iterations:
* 16.832045078278s
stdClass with numeric keys get_object_vars(), 100000000 iterations:
* 17.554226875305s
stdClass with numeric and non-numeric keys to array cast, 100000000 iterations:
* 17.415673017502s
stdClass with numeric and non-numeric keys get_object_vars(), 100000000 iterations:
* 18.347217082977s
Custom class with non-numeric declared properties to array cast, 100000000 iterations:
* 11.410578012466s
Custom class with non-numeric declared properties get_object_vars(), 100000000 iterations:
* 27.630895853043s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment