note 109197 modified in function.create-function by danbrown

From: Date: Mon, 16 Jul 2012 16:06:10 +0000
Subject: note 109197 modified in function.create-function by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189248@lists.php.net to get a copy of this message
Try this to boost performance of your scripts (increase maxCacheSize):&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;runkit_function_copy(&#39;create_function&#39;, &#39;create_function_native&#39;);&#13;&#10;runkit_function_redefine(&#39;create_function&#39;, &#39;$arg,$body&#39;, &#39;return __create_function($arg,$body);&#39;);&#13;&#10;&#13;&#10;function __create_function($arg, $body) {&#13;&#10;&#9;static $cache = array();&#13;&#10;&#9;static $maxCacheSize = 64;&#13;&#10;&#9;static $sorter;&#13;&#10;&#13;&#10;&#9;if ($sorter === NULL) {&#13;&#10;&#9;&#9;$sorter = function($a, $b) {&#13;&#10;&#9;&#9;&#9;if ($a-&#62;hits == $b-&#62;hits) {&#13;&#10;&#9;&#9;&#9;&#9;return 0;&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#13;&#10;&#9;&#9;&#9;return ($a-&#62;hits &#60; $b-&#62;hits) ? 1 : -1;&#13;&#10;&#9;&#9;};&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;$crc = crc32($arg . &#34;\\x00&#34; . $body);&#13;&#10;&#13;&#10;&#9;if (isset($cache[$crc])) {&#13;&#10;&#9;&#9;++$cache[$crc][1];&#13;&#10;&#9;&#9;return $cache[$crc][0];&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;if (sizeof($cache) &#62;= $maxCacheSize) {&#13;&#10;&#9;&#9;uasort($cache, $sorter);&#13;&#10;&#9;&#9;array_pop($cache);&#13;&#10;&#9;}&#13;&#10;&#13;&#10;&#9;$cache[$crc] = array($cb = eval(&#39;return function(&#39;.$arg.&#39;){&#39;.$body.&#39;};&#39;), 0);&#13;&#10;&#9;return $cb;&#13;&#10;}&#13;&#10;?&#62; --was-- Try this to boost performance of your scripts (increase maxCacheSize): <?php runkit_function_copy('create_function', 'create_function_native'); runkit_function_redefine('create_function', '$arg,$body', 'return __create_function($arg,$body);'); function __create_function($arg, $body) { static $cache = array(); static $maxCacheSize = 64; static $sorter; if ($sorter === NULL) { $sorter = function($a, $b) { if ($a->hits == $b->hits) { return 0; } return ($a->hits < $b->hits) ? 1 : -1; }; } $crc = crc32($arg . "\x00" . $body); if (isset($cache[$crc])) { ++$cache[$crc][1]; return $cache[$crc][0]; } if (sizeof($cache) >= $maxCacheSize) { uasort($cache, $sorter); array_pop($cache); } $cache[$crc] = array($cb = eval('return function('.$arg.'){'.$body.'};'), 0); return $cb; } http://php.net/manual/en/function.create-function.php

« previous php.notes (#189248) next »