Page MenuHomePhabricator
Paste P9648

Wikibase Client EntityAccessor::renumber benchmarks
ActivePublic

Authored by hoo on Nov 16 2019, 2:39 PM.
Tags
None
Referenced Files
F31093273: raw.txt
Nov 16 2019, 2:39 PM
Subscribers
None
$ cat test.php
<?php
namespace UsingIsset {
class EntityAccessor {
/**
* Recursively renumber a serialized array in place, so it is indexed at 1, not 0.
* Just like Lua wants it.
*
* @param array &$entityArr
*/
public function renumber( array &$entityArr ) {
foreach ( $entityArr as &$value ) {
if ( !is_array( $value ) ) {
continue;
}
if ( isset( $value[0] ) ) {
array_unshift( $value, null );
unset( $value[0] );
}
$this->renumber( $value );
}
}
}
}
namespace UsingArrayKeyExists {
class EntityAccessor {
/**
* Recursively renumber a serialized array in place, so it is indexed at 1, not 0.
* Just like Lua wants it.
*
* @param array &$entityArr
*/
public function renumber( array &$entityArr ) {
foreach ( $entityArr as &$value ) {
if ( !is_array( $value ) ) {
continue;
}
if ( \array_key_exists( 0, $value ) ) {
array_unshift( $value, null );
unset( $value[0] );
}
$this->renumber( $value );
}
}
}
}
namespace other {
$entityStr = file_get_contents( 'https://www.wikidata.org/wiki/Special:EntityData/Q42.json' );
$entityArr = json_decode( $entityStr, true);
$iEntityAccessor = new \UsingIsset\EntityAccessor();
$akeEntityAccessor = new \UsingArrayKeyExists\EntityAccessor();
$tmp = $entityArr;
$iEntityAccessor->renumber( $tmp );
$tmp = $entityArr;
$akeEntityAccessor->renumber( $tmp );
$t0 = microtime( true );
for ( $i = 0; $i < 1000; $i++ ) {
$tmp = $entityArr;
$iEntityAccessor->renumber( $tmp );
}
echo 'isset-Variant took ' . ( microtime( true ) - $t0 ) . "s\n";
$iRes = $tmp;
$t0 = microtime( true );
for ( $i = 0; $i < 1000; $i++ ) {
$tmp = $entityArr;
$akeEntityAccessor->renumber( $tmp );
}
echo 'array_key_exists-Variant took ' . ( microtime( true ) - $t0 ) . "s\n";
$akeRes = $tmp;
if ( $iRes !== $akeRes ) {
echo "\nMISMATCH!\n";
exit(1);
}
}
$ php --version
PHP 7.3.11 (cli) (built: Oct 22 2019 08:11:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.8.0, Copyright (c) 2002-2019, by Derick Rethans
$ /usr/bin/php test.php
isset-Variant took 5.4207820892334s
array_key_exists-Variant took 6.3041920661926s
$ /usr/bin/php test.php
isset-Variant took 5.5355429649353s
array_key_exists-Variant took 6.3703351020813s
$ /usr/bin/php test.php
isset-Variant took 5.5647039413452s
array_key_exists-Variant took 6.799791097641s
$ php --version
PHP 7.3.11 (cli) (built: Oct 22 2019 08:11:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.11, Copyright (c) 1998-2018 Zend Technologies
$ /usr/bin/php test.php
isset-Variant took 0.76809501647949s
array_key_exists-Variant took 0.79988884925842s
$ /usr/bin/php test.php
isset-Variant took 0.76216292381287s
array_key_exists-Variant took 0.81045484542847s
$ /usr/bin/php test.php
isset-Variant took 0.78294706344604s
array_key_exists-Variant took 0.81283092498779s
$ podman run php:7.4.0RC6-cli php --version
PHP 7.4.0RC6 (cli) (built: Nov 15 2019 08:11:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0649929046631s
array_key_exists-Variant took 1.3193988800049s
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0611300468445s
array_key_exists-Variant took 1.2762169837952s
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0468919277191s
array_key_exists-Variant took 1.3040971755981s
# Change array_key_exists call to \array_key_exists
$ podman run php:7.4.0RC6-cli php --version
PHP 7.4.0RC6 (cli) (built: Nov 15 2019 08:11:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0268149375916s
array_key_exists-Variant took 1.0474579334259s
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0177531242371s
array_key_exists-Variant took 1.0207259654999s
$ podman run --volume /tmp/test.php:/test.php php:7.4.0RC6-cli php /test.php
isset-Variant took 1.0010788440704s
array_key_exists-Variant took 1.0223729610443s

Event Timeline