$value) { $data[$name] = $value; } // As the function returns a reference, the return should always be a // variable. return $data; } // This drupal_static_old() is from drupal-7.0-alpha6 (7.0 HEAD as of 2010-07-16) function &drupal_static_old($name, $default_value = NULL, $reset = FALSE) { static $data = array(), $default = array(); if (!isset($name)) { // All variables are reset. This needs to be done one at a time so that // references returned by earlier invocations of drupal_static() also get // reset. foreach ($default as $name => $value) { $data[$name] = $value; } // As the function returns a reference, the return should always be a // variable. return $data; } if ($reset) { // The reset means the default is loaded. if (array_key_exists($name, $default)) { $data[$name] = $default[$name]; } else { // Reset was called before a default is set and yet a variable must be // returned. return $data; } } elseif (!array_key_exists($name, $data)) { // Store the default value internally and also copy it to the reference to // be returned. $default[$name] = $data[$name] = $default_value; } return $data[$name]; } function test_old($nvars, $nreps) { $varnames = array(); // create $nvars new static variables for ($j = 0; $j < $nvars; $j++) { $varnames[$j] = sprintf("%s_%03d", __FUNCTION__, $j); $var[$j] =& drupal_static_old($varnames[$j]); $var[$j] = $j; } // main loop. recall all $nvars variables, $nreps times $t0 = microtime(TRUE); // mark start time for ($i = 0; $i < $nreps; $i++) { for ($j = 0; $j < $nvars; $j++) { $var[$j] =& drupal_static_old($varnames[$j]); } } $t_elapsed = microtime(TRUE) - $t0; // elapsed seconds $t_microsec = $t_elapsed * 1000000 / ($nvars * $nreps); return $t_microsec;// return microseconds per call } function test_new($nvars, $nreps) { $varnames = array(); // create $nvars new static variables for ($j = 0; $j < $nvars; $j++) { $varnames[$j] = sprintf("%s_%03d", __FUNCTION__, $j); $var[$j] =& drupal_static_new($varnames[$j]); $var[$j] = $j; } // main loop. recall all $nvars variables, $nreps times $t0 = microtime(TRUE); // mark start time for ($i = 0; $i < $nreps; $i++) { for ($j = 0; $j < $nvars; $j++) { $var[$j] =& drupal_static_new($varnames[$j]); } } $t_elapsed = microtime(TRUE) - $t0; // elapsed seconds $t_microsec = $t_elapsed * 1000000 / ($nvars * $nreps); return $t_microsec; // return microseconds per call } function test($nvars, $nreps) { // $nvars is the number of static variables to be created and recalled // $nreps is number of times all $nvars will be called during measurement // The total number of drupal_static calls measured is $nvars * $nreps. static $testnum = 0; // current test number $msg = sprintf("%2d: ", ++$testnum);// assemble output string $ncalls = $nvars * $nreps; // number of calls made $msg .= sprintf("nVars=%4d nCalls=%6d ", $nvars, $ncalls); $t_old = test_old($nvars, $nreps); $msg .= sprintf("t_old=%5.1fus ", $t_old); $t_new = test_new($nvars, $nreps); $msg .= sprintf("t_new=%5.1fus ", $t_new); $gain = 100.0 * ($t_old - $t_new) / $t_old; $speedX = $t_old / $t_new; $msg .= sprintf("gain=%3.1f%% speed=%5.2fx\n", $gain, $speedX); echo($msg); } // Ok run a series of tests with 100000 calls each. (This value determined by // trial and error to take about a second to rum on a Athlon64 3700+ WinXP box.) // Note that multiple tests should be run with increasing values of $nvars, // because drupal_static "remembers" all values stored from the previous run. // nVar nReps test( 1,100000); test( 5, 20000); test( 10, 10000); test( 50, 2000); test( 100, 1000); test( 500, 200); test(1000, 100); ?>