Guest User

Untitled

a guest
Apr 16th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1.         $segment = 10000;
  2.        
  3.         $maxValue = 10000;
  4.        
  5.         for($j = 1; $j < 10; $j++)
  6.         {
  7.             $operations = $segment*$j;
  8.            
  9.             $size = 0;
  10.            
  11.             $set = new HashSet();
  12.            
  13.             $start = microtime(true);
  14.            
  15.             for($i = 0; $i < $operations; $i++)
  16.             {
  17.                 $value = rand(1, $maxValue);
  18.                 if($set->add($value))
  19.                 {
  20.                     $size++;
  21.                 }
  22.             }
  23.            
  24.             $end = microtime(true);
  25.            
  26.             echo $size;
  27.                        
  28.             $duration = ($end-$start)*1000;
  29.            
  30.             echo "\nHashSet: {$operations}, {$duration}";
  31.                        
  32.             $arraySet = [];
  33.            
  34.             $size = 0;
  35.            
  36.             $start = microtime(true);
  37.            
  38.             for($i = 0; $i < $operations; $i++)
  39.             {
  40.                 $value = rand(1, $maxValue);
  41.                 if(!in_array($value, $arraySet))
  42.                 {
  43.                     $arraySet[] = $value;
  44.                     $size++;
  45.                 }
  46.             }
  47.            
  48.             $end = microtime(true);
  49.                        
  50.             $duration = ($end-$start)*1000;
  51.            
  52.             echo "\narray {$operations}, {$duration}";
  53.            
  54.             $ice = new Set();
  55.              
  56.             $size = 0;
  57.              
  58.             $start = microtime(true);
  59.              
  60.             for($i = 0; $i < $operations; $i++)
  61.             {
  62.                 $value = rand(1, $maxValue);
  63.                 if($ice->add($value))
  64.                 {
  65.                     $size++;
  66.                 }
  67.             }
  68.              
  69.             $end = microtime(true);
  70.              
  71.             $this->assertEquals($size, $ice->size());
  72.              
  73.             $duration = ($end-$start)*1000;
  74.            
  75.             echo "\nIce: {$operations}, {$duration}";
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment