Skip to content

Instantly share code, notes, and snippets.

@LionsAd
Created October 25, 2012 03:45
Show Gist options
  • Save LionsAd/3950296 to your computer and use it in GitHub Desktop.
Save LionsAd/3950296 to your computer and use it in GitHub Desktop.
Test array performance in difference scenarios
<?php
require_once 'core/lib/Drupal/Core/Template/TwigReference.php';
use Drupal\Core\Template\TwigReference;
function getAttribute($val, $key) {
//$_GLOBALS['ref'] = &$val[$key];
return $val[$key];
}
function getContext($context, $key) {
// $_GLOBALS['ref'] = $context[$key];
return $context[$key];
}
function getContextReference($context, $key) {
static $twig_reference;
if (!isset($twig_reference)) {
$twig_reference = new TwigReference();
}
// $_GLOBALS['ref'] = $context[$key];
$ref = clone $twig_reference;
$ref->setReference($context[$key]);
return $ref; //$context[$key];
// return $context[$key];
}
function getContextReference2($context, $key) {
$_GLOBALS['ref'] = &$context[$key];
return $_GLOBALS['ref'];
}
function getAttribute2($val, $key) {
$_GLOBALS['ref'] = &$val[$key];
return $_GLOBALS['ref'];
}
function getContextVars($context, $keys) {
$x = $context;
foreach ($keys as $key) {
$x=$x[$key];
}
return $x;
}
function getContextVars2($context, $key1, $key2) {
$x = $context[$key1];
$y = $x[$key2];
return $y;
}
function getAttributeRef(&$ref, $key) {
$ref=&$ref[$key];
}
function getContextRef(&$ref, $context, $key) {
$ref = &$context[$key];
}
function &getAttributeRef2(&$ref, $key) {
return $ref[$key];
}
function &getContextRef2($context, $key) {
return $context[$key];
}
function hide(&$x) {
$x['#hidden'] = TRUE;
}
$context['x']['y']['z']='hi';
$j=1;
for ( $i = 0; $i < 500000; $i++ )
{
$context['x']['y'][$i]['data']=TRUE;
}
$time_start = microtime( true );
class X {
private $state = null;
public function getContext($context, $key) {
$this->state = $context[$key];
return $this;
}
public function getAttribute($key) {
$this->state = $this->state[$key];
return $this;
}
public function getValue() {
return $this->state;
}
function getAttributeRef(&$ref, $key) {
$ref=&$ref[$key];
}
function getContextRef(&$ref, $context, $key) {
$ref = &$context[$key];
}
}
$x = new X();
for ( $i = 0; $i < 100000; $i++ )
{
$j = $i;
// $_x = $context['x'];
// $y = getAttribute($_x, 'y');
//$y = getAttribute(getContextReference($context, 'x'),'y');
// $y = getAttribute2(getContextReference2($context, 'x'),'y');
// $y = getAttribute(getContext($context, 'x'),'y');
// $y = getContextVars2($context, 'x','y');
//$y = $context['x']['y'];
// getAttributeRef2(getAttributeRef2(getContextRef2($context, 'x'),'y'), $j);
/*
$_tmp = getContextRef2($context, 'x');
$_tmp = getAttributeRef2($_tmp, 'y');
$_tmp = getAttributeRef2($_tmp, $j);
hide($_tmp);
*/
//$y = getAttribute(getContextReference($context, 'x'),'y');
//$y = getAttribute2(getContextReference2($context, 'x'),'y');
//$_tmp = getAttribute(getAttribute(getContextReference($context, 'x'),'y'), $j);
$_tmp = getAttribute(getAttribute(getContext($context, 'x'),'y'), $j);
hide($_tmp);
/*
$_tmp=getContext($context, 'x');
$_tmp=getAttribute($_tmp, 'y');
$_tmp=getAttribute($_tmp, $j);
hide($_tmp);
*/
/*
getContextRef($_tmp, $context, 'x');
getAttributeRef($_tmp, 'y');
getAttributeRef($_tmp, $j);
hide($_tmp);
*/
/*
$_tmp = $x->getContext($context, 'x')->getAttribute('y')->getAttribute($j)->getValue();
hide($_tmp);
*/
/*
$x->getContextRef($_tmp, $context, 'x');
$x->getAttributeRef($_tmp, 'y');
$x->getAttributeRef($_tmp, $j);
hide($_tmp);
*/
//$_tmp=getContext($context, 'x');
//$_tmp=getAttribute($_tmp, 'y');
//$y = $_tmp;
}
// calculate total running time and output the result
$time_end = ( microtime( true ) - $time_start );
printf( "loop time: %fs\n",
$time_end
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment