SHARE
TWEET

PHP Factorial testing

agrochal Jan 18th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. set_time_limit(30000000);
  3. function factorial($start, $end, $calculation, $grow)
  4. {
  5.     $calculation = strval($calculation);
  6.     if(!$grow) {
  7.         $tmp = $start;
  8.         $start = $end;
  9.         $end = $tmp;
  10.     }
  11.     for($i=$start+1;$i<=$end;$i++){
  12.         if($grow) {
  13.             $calculation = strval($calculation) * strval($i);
  14.         } else {
  15.             $calculation = strval($calculation) / strval($i);
  16.         }
  17.  
  18.     }
  19.     return strval($calculation);
  20. }
  21.  
  22.  
  23. $numbers = 1000;
  24. for($x=1;$x<=$numbers;$x++){
  25.     $noncache = 0;
  26.     $cache = 0;
  27.     $array = [];
  28.     for($i=0;$i<$x;$i++){
  29.         $random = rand(1, 170);
  30.         array_push($array, $random);
  31.     }
  32.  
  33.     $start = microtime(true);
  34.     for($i=0;$i<$x;$i++){
  35.         $calculation = "1";
  36.         $limit = $array[$i];
  37.         $calculation = factorial(0, $limit, 1, true);
  38.     }
  39.     $time_elapsed_milisecs = microtime(true) - $start;
  40.     $noncache = round($time_elapsed_milisecs*1000,3);
  41.  
  42.     $lastnumber = null;
  43.     $lastcalculation = null;
  44.     $start = microtime(true);
  45.     for($i=0;$i<$x;$i++){
  46.         $limit = $array[$i];
  47.         if($lastnumber==null||$lastcalculation==null) {
  48.             $calculation = factorial(0, $limit, 1, true);
  49.         } else {
  50.             $calculation = factorial($lastnumber, $limit, $lastcalculation, $lastnumber<$limit);
  51.         }
  52.         $lastcalculation = $calculation;
  53.         $lastnumber = $limit;
  54.  
  55.     }
  56.     $time_elapsed_milisecs = microtime(true) - $start;
  57.     $cache = round($time_elapsed_milisecs*1000,3);
  58.     echo $x . ":" . $noncache . ":" . $cache . '<br>';
  59.     unset($array);
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top