Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active December 15, 2015 05:29
Show Gist options
  • Save ziadoz/5209082 to your computer and use it in GitHub Desktop.
Save ziadoz/5209082 to your computer and use it in GitHub Desktop.
PHP Memoize and Benchmark Primes
<?php
$prime = function($num) {
if ($num === 1) {
return false;
}
if ($num === 2) {
return true;
}
if ($num % 2 === 0) {
return false;
}
$total = ceil(sqrt($num));
for ($i = 3; $i < $total; $i = $i + 2) {
if ($num % $i === 0) {
return false;
}
}
return true;
};
$range = function($start, $end, $function) {
return function() use ($start, $end, $function) {
$results = array();
for ($i = $start; $i <= $end; $i++) {
if ($function($i) === true) {
$results[] = $i;
}
}
return $results;
};
};
$memoize = function($function) {
static $cache = array();
return function() use (&$cache, $function) {
$args = func_get_args();
$md5 = md5(implode('', $args));
if (isset($cache[$md5])) {
return $cache[$md5];
}
$cache[$md5] = call_user_func_array($function, $args);
return $cache[$md5];
};
};
$benchmark = function($function) {
return function() use ($function) {
$start = microtime(true);
$result = call_user_func_array($function, func_get_args());
$end = microtime(true);
echo 'Time Elapsed: ' . ($end - $start) . "\n";
return $result;
};
};
echo '<pre>';
$start = 1;
$end = 1000;
$primes = $benchmark($range($start, $end, $memoize($prime)));
print_r($primes());
$primes = $benchmark($range($start, $end, $memoize($prime)));
print_r($primes());
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment