Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2. class Human extends Animal
  3. {
  4. }
  5.  
  6. class Animal
  7. {
  8.     protected $skin;
  9.  
  10.     function __construct()
  11.     {
  12.         $this->skin = rand(1, 10000);
  13.     }
  14.  
  15.     function getSkin()
  16.     {
  17.         return $this->skin;
  18.     }
  19. }
  20.  
  21. function make_random_array($count, $class)
  22. {
  23.     $arr = [];
  24.     for ($i = 0; $i < $count; $i++) {
  25.         $arr[] = new $class();
  26.     }
  27.     return $arr;
  28. }
  29.  
  30. $humans = make_random_array(300000, 'Human');
  31.  
  32. $ignored = make_random_array(2000, 'Animal');
  33.  
  34. $start = microtime(true);
  35.  
  36. $ignored_skins = [];
  37. foreach ($ignored as $skin){
  38.     $ignored_skins[$skin->getSkin()] = true;
  39. }
  40.  
  41. $suitable_humans = [];
  42. foreach($humans as $humen){
  43.     if(!isset($ignored_skins[$humen->getSkin()])){
  44.         $suitable_humans[] = $humen;
  45.     }
  46. }
  47.  
  48. $end = microtime(true);
  49.  
  50. echo 'done for ' . ($end - $start) . ' ms' . PHP_EOL;
  51. echo 'out of ' . count($humans) . ' humans ' . count($suitable_humans) . ' are have suitable skin' . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement