Last active
December 14, 2015 02:39
-
-
Save patrickallaert/5015153 to your computer and use it in GitHub Desktop.
Comparing CPU time and memory usage between classes and arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$i = 0; | |
$x = []; | |
$start = microtime( true ); | |
do { | |
$x[] = array( "firstname" => "Patrick", "lastname" => "Allaert" ); | |
++$i; | |
} while ( $i < 500000 ); | |
echo microtime( true ) - $start, "\n", memory_get_peak_usage(), "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$i = 0; | |
$x = []; | |
class person { | |
public $firstname; | |
public $lastname; | |
} | |
$start = microtime( true ); | |
do { | |
$p = new person; | |
$p->firstname = "Patrick"; | |
$p->lastname = "Allaert"; | |
$x[] = $p; | |
++$i; | |
} while ( $i < 500000 ); | |
echo microtime( true ) - $start, "\n", memory_get_peak_usage(), "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$i = 0; | |
$x = []; | |
class person { | |
public $firstname; | |
public $lastname; | |
public function __construct( $firstname, $lastname ) { | |
$this->firstname = $firstname; | |
$this->lastname = $lastname; | |
} | |
} | |
$start = microtime( true ); | |
do { | |
$x[] = new person( "Patrick", "Allaert" ); | |
++$i; | |
} while ( $i < 500000 ); | |
echo microtime( true ) - $start, "\n", memory_get_peak_usage(), "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP 5.4: | |
======== | |
$ php54 bench1.php | |
0.36937880516052 | |
292432712 | |
$ php54 bench2.php | |
0.47057294845581 | |
193929016 | |
$ php54 bench3.php | |
0.79254007339478 | |
193944896 | |
PHP 5.3: | |
======== | |
$ php53 bench1.php | |
1.0838158130646 | |
684949896 | |
$ php53 bench2.php | |
1.8599081039429 | |
774477240 | |
$ php53 bench3.php | |
2.2024641036987 | |
774479960 | |
Arrays takes much more space than equivalent struct classes but are slighly faster in *PHP 5.4*. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment