Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a generic <a href="http://en.wikipedia.org/wiki/Memoization" rel="noreferrer">memoization</a> solution that is compatible with PHP 5.2.x.</p> <p>The call_memoized function:</p> <ol> <li>Generates a unique cache key based on the function name and arguments.</li> <li>If the cache is empty, invokes the function and updates the cache with the result.</li> <li>Returns the cached value.</li> </ol> <p>Instead of using call_memoized directly, use the provided memoize function, which returns a new version of the given function that will automatically cache results from the same combination of parameters.</p> <p>See example below.</p> <p>Enjoy!</p> <pre class="lang-php prettyprint-override"><code>global $MEMO_CACHE; $MEMO_CACHE = array(); /** Returns the result of the function with the given arguments. * Invokes the function only once, thereafter returns the result * cached by a key based on the function name and arguments. */ function call_memoized($fun, $args=array()) { global $MEMO_CACHE; // generate a cache key based on the function name and arguments $uid = md5( implode("|", array_merge((array)$fun, array_map( "serialize", $args) )) ); // if there result hasn't been cached, call the function // and update the cache with the result. if(!array_key_exists($uid, $MEMO_CACHE)) { $MEMO_CACHE[$uid] = call_user_func_array($fun, $args); } return $MEMO_CACHE[$uid]; } /** Returns a memoized version of the given function that will cache * its results for each unique set of inputs. */ function memoize($fun) { return create_function( '', "\$args = func_get_args(); return call_memoized('$fun', \$args);" ); } </code></pre> <p><strong>Example:</strong></p> <pre class="lang-php prettyprint-override"><code>/** Returns a random number with the given greeting. */ function random($greeting) { return "$greeting! " . rand(); }; print("Five random numbers:&lt;/br /&gt;"); for($i=0; $i&lt;5; $i++) { print(random("Spin the wheel") . "&lt;br /&gt;"); } print "&lt;br /&gt;"; print("After memoizing the random function, it's not so random:&lt;br /&gt;"); $not_so_random = memoize("random"); for($i=0; $i&lt;5; $i++) { print($not_so_random("Spin the wheel") . "&lt;br /&gt;"); } print "&lt;br /&gt;"; print("The same memoized function is invoked with a different argument, and thus creates a different cache key:&lt;br /&gt;"); for($i=0; $i&lt;5; $i++) { print($not_so_random("Twirl the tire") . "&lt;br /&gt;"); } /* OUTPUT Five random numbers: Spin the wheel! 26488 Spin the wheel! 20049 Spin the wheel! 14006 Spin the wheel! 28599 Spin the wheel! 804 After memoizing the random function, it's not so random: Spin the wheel! 32397 Spin the wheel! 32397 Spin the wheel! 32397 Spin the wheel! 32397 Spin the wheel! 32397 The same memoized function is invoked with a different argument, and thus creates a different cache key: Twirl the tire! 2114 Twirl the tire! 2114 Twirl the tire! 2114 Twirl the tire! 2114 Twirl the tire! 2114 */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload