Advertisement
mython

PHP esperimenti

Apr 6th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. Output:
  2. normale: 0.54769611358643 s
  3. veloce: 0.0072240829467773 s
  4.  
  5. Codice:
  6. <?php
  7.     function normale() {
  8.         $square_vals = array();
  9.         for ($i = 0; $i < 23; $i++) {
  10.             $square_vals[$i] = range($i, $i + 3);
  11.         }
  12.  
  13.         $t0 = microtime(true);
  14.         for ($i = 0; $i < 100000; $i++) {
  15.             $n = $i % 36;
  16.             $ret = array();
  17.             foreach ($square_vals as $value) {
  18.                 if (in_array($n, $value)) {
  19.                     array_push($ret, $i);
  20.                 }
  21.             }
  22.         }
  23.         $delta = microtime(true) - $t0;
  24.         echo "normale: $delta s";
  25.     }
  26.  
  27.     function veloce() {
  28.         $square_vals = array();
  29.         for ($i = 0; $i < 37; $i++) {
  30.             $square_vals[$i] = range($i, $i + 2);
  31.         }
  32.  
  33.         $t0 = microtime(true);
  34.         for ($i = 0; $i < 100000; $i++) {
  35.             $n = $i % 36;
  36.             $ret = $square_vals[$n];
  37.         }
  38.         $delta = microtime(true) - $t0;
  39.         echo "veloce: $delta s";
  40.     }
  41.  
  42.     normale();
  43.     echo "<br>";
  44.     veloce();
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement