PHP code example of kanellov / memoize

1. Go to this page and download the library: Download kanellov/memoize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kanellov / memoize example snippets





tion benchmark($name, $runs, $function)
{
    $start = microtime(true);
    while ($runs--) {
        $function();
    }
    $end = microtime(true);

    return sprintf('%s: %s', $name, ($end - $start)) . PHP_EOL;
}

function heavyCalc($varA, $varB)
{
    usleep(100);
    return $varA + $varB;
}

$memoized = Knlv\memoize('heavyCalc');

echo benchmark('heavyCalc(1, 2)', 100, function() {
    heavyCalc(1, 2);
});

echo benchmark('Memoized heavyCalc(1, 2)', 100, function () use (&$memoized) {
    $memoized(1, 2);
});

/*
heavyCalc(1, 2): 0.016629219055176
Memoized heavyCalc(1, 2): 0.001600980758667
*/