Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class timer {
- private $start_time = NULL;
- private $end_time = NULL;
- private function getmicrotime() {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- function start() {
- $this->start_time = $this->getmicrotime();
- }
- function stop() {
- $this->end_time = $this->getmicrotime();
- }
- function result() {
- if (is_null($this->start_time)) {
- exit('Timer: start method not called !');
- return false;
- }
- elseif (is_null($this->end_time)) {
- exit('Timer: stop method not called !');
- return false;
- }
- return round(($this->end_time - $this->start_time), 4);
- }
- }
- function genRand($length = 10, $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $chars[rand(0, strlen($chars) - 1)];
- }
- return $randomString;
- }
- function populate($count = 10000) {
- $array = array();
- for($i = 0; $i < $count; $i++) {
- $phone = genRand(5,'0123456789');
- $array[$phone] = array(
- 'phone' => $phone,
- 'name' => genRand(20),
- 'age' => genRand(2, '0123456789'),
- );
- }
- return $array;
- }
- $array1 = populate(40000);
- $array2 = populate(40000);
- $match = FALSE;
- $timer = new timer();
- $timer->start();
- foreach($array1 as $key => $value) {
- if(array_key_exists($key, $array2)) {
- $match = TRUE;
- break;
- }
- }
- $timer->stop();
- print_r('Match '.($match ? '' : 'not')." found\n");
- // show result now
- print_r('For Each completed in '.$timer->result()."\n");
- $timer = new timer();
- $timer->start();
- $matched = array_intersect_key($array1, $array2);
- $timer->stop();
- // show result now
- print_r('Intersect Key completed in '.$timer->result()."\n"); // At least 100x longer...
Advertisement
Add Comment
Please, Sign In to add comment