Guest User

Untitled

a guest
Jul 16th, 2014
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2.  
  3. class timer {
  4.     private $start_time = NULL;
  5.     private $end_time = NULL;
  6.  
  7.     private function getmicrotime() {
  8.       list($usec, $sec) = explode(" ", microtime());
  9.       return ((float)$usec + (float)$sec);
  10.     }
  11.  
  12.     function start() {
  13.       $this->start_time = $this->getmicrotime();
  14.     }
  15.  
  16.     function stop() {
  17.       $this->end_time = $this->getmicrotime();
  18.     }
  19.  
  20.     function result() {
  21.         if (is_null($this->start_time)) {
  22.             exit('Timer: start method not called !');
  23.             return false;
  24.         }
  25.         elseif (is_null($this->end_time)) {
  26.             exit('Timer: stop method not called !');
  27.             return false;
  28.         }
  29.  
  30.         return round(($this->end_time - $this->start_time), 4);
  31.     }
  32.  
  33. }
  34.  
  35. function genRand($length = 10, $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
  36.     $randomString = '';
  37.     for ($i = 0; $i < $length; $i++) {
  38.         $randomString .= $chars[rand(0, strlen($chars) - 1)];
  39.     }
  40.     return $randomString;
  41. }
  42.  
  43. function populate($count = 10000) {
  44.         $array = array();
  45.         for($i = 0; $i < $count; $i++) {
  46.                 $phone = genRand(5,'0123456789');
  47.                 $array[$phone] = array(
  48.                         'phone' => $phone,
  49.                         'name' => genRand(20),
  50.                         'age' => genRand(2, '0123456789'),
  51.                 );
  52.         }
  53.         return $array;
  54. }
  55.  
  56. $array1 = populate(40000);
  57. $array2 = populate(40000);
  58.  
  59. $match = FALSE;
  60.  
  61. $timer = new timer();
  62.  
  63. $timer->start();
  64.  
  65. foreach($array1 as $key => $value) {
  66.     if(array_key_exists($key, $array2)) {
  67.         $match = TRUE;
  68.         break;
  69.     }
  70. }
  71.  
  72. $timer->stop();
  73.  
  74. print_r('Match '.($match ? '' : 'not')." found\n");
  75.  
  76. // show result now
  77. print_r('For Each completed in '.$timer->result()."\n");
  78.  
  79. $timer = new timer();
  80.  
  81. $timer->start();
  82.  
  83. $matched = array_intersect_key($array1, $array2);
  84.  
  85. $timer->stop();
  86.  
  87. // show result now
  88. print_r('Intersect Key completed in '.$timer->result()."\n"); // At least 100x longer...
Advertisement
Add Comment
Please, Sign In to add comment