Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(15);
- function a1()
- {
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i = 0;
- echo "<pre>";
- echo "//GOsha:".PHP_EOL;
- $s1 = microtime();
- $groups = array(range(0,18),range(19,30),range(31,50),range(51,100));
- $result = array(0=>0,1=>0,2=>0,3=>0);
- foreach($ages as $age)
- {
- foreach($groups as $k=>$group)
- {
- $i++;
- if(in_array($age,$group)){
- @$result[$k]++;
- break;
- }
- }
- }
- print_r($result);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- function a2()
- {
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i=0;
- echo "<pre>";
- echo "//vitaliyp:".PHP_EOL;
- $s1 = microtime();
- $groupped = array('0-18'=>0,'18-30'=>0,'30-50'=>0,'50-100'=>0);
- foreach($ages as $a){
- switch($a){
- case in_array($a,range(0,18)):
- $groupped['0-18']++;
- break;
- case in_array($a,range(18,30)):
- $groupped['18-30']++;
- break;
- case in_array($a,range(30,50)):
- $groupped['30-50']++;
- break;
- case in_array($a,range(50,100)):
- $groupped['50-100']++;
- break;
- }
- $i++;
- }
- print_r($groupped);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- function a3()
- {
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i = 0;
- echo "<pre>";
- echo "//sectus:".PHP_EOL;
- $s1 = microtime();
- $all_ages = array_count_values($ages) + array_fill(0, 101, 0);
- ksort($all_ages);
- $groupped['0-18'] = array_sum(array_slice($all_ages, 0, 19));
- $groupped['19-30']= array_sum(array_slice($all_ages, 19, 12));
- $groupped['31-50']= array_sum(array_slice($all_ages, 31, 20));
- $groupped['51-100']= array_sum(array_slice($all_ages, 51, 50));
- print_r($groupped);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- function a4()
- {
- $result = array('18'=>0,'30'=>0,'50'=>0,'100'=>0);
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i = 0;
- echo "<pre>";
- echo "//evts85:".PHP_EOL;
- $s1 = microtime();
- foreach($ages as $age){
- foreach($result as $key => $value){
- $i++;
- if($age < $key){
- $result[$key]++;
- break;
- }
- }
- }
- print_r($result);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- function a5()
- {
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i = 0;
- echo "<pre>";
- echo "//tempman:".PHP_EOL;
- $s1 = microtime();
- rsort($ages);
- $rages = array('18'=>0,'30'=>0,'50'=>0,'100'=>0);
- $ages_count = count($ages);
- $prev_pos = 0;
- $prev_limit = 0;
- foreach ($rages as $k => $v){
- $index = $k; //
- do{
- $i++;
- $pos = array_search($index,$ages);
- if($pos!==false)
- {
- $prev_pos += $rages[$k]= $ages_count - $pos - $prev_pos;
- };
- $index--;
- }while($pos===false && ($index > $prev_limit));
- $prev_limit = $v;
- }
- print_r($rages);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- function a6()
- {
- $ages = array(48,24,23,12,45,56,67,33,35,42,12,14,15,27);
- $i = 0;
- echo "<pre>";
- echo "//if..else:".PHP_EOL;
- $s1 = microtime();
- $result = array(0=>0,1=>0,2=>0,3=>0);
- foreach($ages as $age)
- {
- $i++;
- if($age <=18) $result[0]++;
- if($age >18 && $age<=30) $result[1]++;
- if($age >30 && $age<=50) $result[2]++;
- if($age >50 && $age<=100) $result[3]++;
- }
- print_r($result);
- $s2 = microtime();
- $e = $s2-$s1;
- echo "time:".$e.PHP_EOL;
- echo "iterations:".$i.PHP_EOL;
- echo "</pre>";
- }
- a1();
- a2();
- a3();
- a4();
- a5();
- a6();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement