Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2. error_reporting(15);
  3. function a1()
  4. {
  5.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  6.     $i = 0;
  7.     echo "<pre>";
  8.     echo "//GOsha:".PHP_EOL;
  9.     $s1 = microtime();
  10.     $groups = array(range(0,18),range(19,30),range(31,50),range(51,100));
  11.     $result = array(0=>0,1=>0,2=>0,3=>0);
  12.     foreach($ages as $age)
  13.     {
  14.         foreach($groups as $k=>$group)
  15.         {
  16.             $i++;
  17.             if(in_array($age,$group)){
  18.                 @$result[$k]++;
  19.                 break;
  20.             }
  21.         }
  22.     }
  23.     print_r($result);
  24.  
  25.     $s2 = microtime();
  26.     $e = $s2-$s1;
  27.     echo "time:".$e.PHP_EOL;
  28.     echo "iterations:".$i.PHP_EOL;
  29.     echo "</pre>";
  30. }
  31.  
  32. function a2()
  33. {
  34.    
  35.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  36.     $i=0;
  37.     echo "<pre>";
  38.     echo "//vitaliyp:".PHP_EOL;
  39.     $s1 = microtime();
  40.     $groupped = array('0-18'=>0,'18-30'=>0,'30-50'=>0,'50-100'=>0);
  41.     foreach($ages as $a){
  42.       switch($a){
  43.         case in_array($a,range(0,18)):
  44.           $groupped['0-18']++;
  45.           break;
  46.         case in_array($a,range(18,30)):
  47.           $groupped['18-30']++;
  48.           break;
  49.         case in_array($a,range(30,50)):
  50.           $groupped['30-50']++;
  51.           break;
  52.         case in_array($a,range(50,100)):
  53.           $groupped['50-100']++;
  54.           break;
  55.       }
  56.       $i++;
  57.     }
  58.  
  59.     print_r($groupped);
  60.     $s2 = microtime();
  61.     $e = $s2-$s1;
  62.     echo "time:".$e.PHP_EOL;
  63.     echo "iterations:".$i.PHP_EOL;
  64.     echo "</pre>";
  65. }
  66.  
  67. function a3()
  68. {
  69.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  70.     $i = 0;
  71.     echo "<pre>";
  72.     echo "//sectus:".PHP_EOL;
  73.     $s1 = microtime();
  74.     $all_ages = array_count_values($ages) + array_fill(0, 101, 0);
  75.     ksort($all_ages);
  76.     $groupped['0-18'] = array_sum(array_slice($all_ages, 0, 19));
  77.     $groupped['19-30']= array_sum(array_slice($all_ages, 19, 12));
  78.     $groupped['31-50']= array_sum(array_slice($all_ages, 31, 20));
  79.     $groupped['51-100']= array_sum(array_slice($all_ages, 51, 50));
  80.     print_r($groupped);
  81.     $s2 = microtime();
  82.     $e = $s2-$s1;
  83.     echo "time:".$e.PHP_EOL;
  84.     echo "iterations:".$i.PHP_EOL;
  85.     echo "</pre>";
  86. }
  87.  
  88. function a4()
  89. {
  90.     $result = array('18'=>0,'30'=>0,'50'=>0,'100'=>0);
  91.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  92.     $i = 0;
  93.     echo "<pre>";
  94.     echo "//evts85:".PHP_EOL;
  95.     $s1 = microtime();
  96.     foreach($ages as $age){
  97.         foreach($result as $key => $value){
  98.             $i++;
  99.             if($age < $key){
  100.                 $result[$key]++;
  101.                 break;
  102.             }
  103.          }
  104.     }
  105.     print_r($result);
  106.     $s2 = microtime();
  107.     $e = $s2-$s1;
  108.     echo "time:".$e.PHP_EOL;
  109.     echo "iterations:".$i.PHP_EOL;
  110.     echo "</pre>";
  111. }
  112.  
  113. function a5()
  114. {
  115.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  116.     $i = 0;
  117.     echo "<pre>";
  118.     echo "//tempman:".PHP_EOL;
  119.     $s1 = microtime();
  120.     rsort($ages);
  121.     $rages = array('18'=>0,'30'=>0,'50'=>0,'100'=>0);
  122.     $ages_count = count($ages);
  123.     $prev_pos = 0;
  124.     $prev_limit = 0;
  125.     foreach ($rages as $k => $v){
  126.  
  127.       $index = $k; //
  128.       do{
  129.         $i++;      
  130.         $pos = array_search($index,$ages);
  131.         if($pos!==false)
  132.         {
  133.      
  134.         $prev_pos += $rages[$k]= $ages_count - $pos - $prev_pos;
  135.         };
  136.         $index--;
  137.  
  138.       }while($pos===false  && ($index > $prev_limit));
  139.       $prev_limit = $v;
  140.     }
  141.     print_r($rages);
  142.     $s2 = microtime();
  143.     $e = $s2-$s1;
  144.     echo "time:".$e.PHP_EOL;
  145.     echo "iterations:".$i.PHP_EOL;
  146.     echo "</pre>";
  147. }
  148.  
  149. function a6()
  150. {
  151.     $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
  152.     $i = 0;
  153.     echo "<pre>";
  154.     echo "//if..else:".PHP_EOL;
  155.     $s1 = microtime();
  156.     $result = array(0=>0,1=>0,2=>0,3=>0);
  157.     foreach($ages as $age)
  158.     {
  159.         $i++;
  160.         if($age <=18) $result[0]++;
  161.         if($age >18 && $age<=30) $result[1]++;
  162.         if($age >30 && $age<=50) $result[2]++;
  163.         if($age >50 && $age<=100) $result[3]++;
  164.     }
  165.     print_r($result);
  166.  
  167.     $s2 = microtime();
  168.     $e = $s2-$s1;
  169.     echo "time:".$e.PHP_EOL;
  170.     echo "iterations:".$i.PHP_EOL;
  171.     echo "</pre>";
  172. }
  173.  
  174. a1();
  175. a2();
  176. a3();
  177. a4();
  178. a5();
  179. a6();
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement