$a = 1 ; // $b = &$a ; // $b $a echo $b ; // 1
class Main { public $List = Array(); // public $Links = Array(); // public $Count = NULL ; // public function Fill() // { for( $i=0; $i < $this->Count; $i++ ) { $Obj = &$this->List[]; // $Obj = new Obj($i); $Key = md5($i); $this->Links[$Key] = &$Obj; // } } } class Obj // { public $Id = NULL; public function __construct( $Id ) { $this->Id = $Id; } } $Main = new Main(); $Main->Count = 1000; // , $Main->Fill(); /* , print_r( $Main->List ): Array ( [0] => Obj Object ( [Id] => 0 ) [1] => Obj Object ( [Id] => 1 ) [2] => Obj Object ( [Id] => 2 ) ... ) */
class Main { ... public function Search_Cycle( $Id ) { $Limit = count( $this->List ); for( $i=0; $i < $Limit; $i++ ) if( $this->List[$i]->Id == $Id ) return $this->List[$i]; return NULL; } public function Search_Link( $Id ) { $Key = md5($i); if( isset( $this->Links[$Key] ) == TRUE ) return $this->Links[$Key]; return NULL; } ... }
... $Count = 100 ; // // 10 for( $t=0; $t < $Main->Count; $t++ ) { echo 'Test with ID: ' . $t . ' - started.'; // , $Start = microtime( TRUE ); for( $i=0; $i < $Count; $i++ ) $Main->Search_Cycle( $t ); $Time = round( microtime( TRUE ) - $Start , 4 ); echo '<br />Search_Cycle() time: ' . $Time; // , $Start = microtime( TRUE ); for( $i=0; $i < $Count; $i++ ) $Main->Search_Link( $t ); $Time = round( microtime( TRUE ) - $Start , 4 ); echo '<br /> Search_Link() time: ' . $Time; echo '<br /><br />'; }
class Main { public $List = Array(); // public $Links = Array(); // public $Count = NULL ; // public function Fill() // { for( $i=0; $i < $this->Count; $i++ ) { $Obj = &$this->List[]; // $Obj = new Obj($i); $Key = md5($i); $this->Links[$Key] = &$Obj; // } } public function Search_Cycle( $Id ) { $Limit = count( $this->List ); for( $i=0; $i < $Limit; $i++ ) if( $this->List[$i]->Id == $Id ) return $this->List[$i]; return NULL; } public function Search_Link( $Id ) { $Key = md5($Id); if( isset( $this->Links[$Key] ) == TRUE ) return $this->Links[$Key]; return NULL; } } class Obj // { public $Id = NULL; public function __construct( $Id ) { $this->Id = $Id; } } $Main = new Main(); $Main->Count = 1000; // , $Main->Fill(); /* , print_r( $Main->List ): Array ( [0] => Obj Object ( [Id] => 0 ) [1] => Obj Object ( [Id] => 1 ) [2] => Obj Object ( [Id] => 2 ) ... ) */ $Count = 100 ; // // 10 for( $t=0; $t < $Main->Count; $t++ ) { echo 'Test with ID: ' . $t . ' - started.'; // , $Start = microtime( TRUE ); for( $i=0; $i < $Count; $i++ ) $Main->Search_Cycle( $t ); $Time = round( microtime( TRUE ) - $Start , 4 ); echo '<br />Search_Cycle() time: ' . $Time; // , $Start = microtime( TRUE ); for( $i=0; $i < $Count; $i++ ) $Main->Search_Link( $t ); $Time = round( microtime( TRUE ) - $Start , 4 ); echo '<br /> Search_Link() time: ' . $Time; echo '<br /><br />'; }
Source: https://habr.com/ru/post/166601/
All Articles