Last active
March 26, 2022 11:34
-
-
Save charrondev/be56ef53e0408b959eff6d2d911e8c1e to your computer and use it in GitHub Desktop.
PHP Object vs Array memory usage with random values
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 | |
include_once "./randomString.php"; | |
$s = []; | |
for ($x = 0; $x < 100000; $x++) { | |
$obj = new stdClass(); | |
$obj->name = randomString(); | |
$obj->age = rand(1, 100); | |
$s[] = $obj; | |
} | |
echo "stdClass objects only peak memory usage: " . memory_get_peak_usage() . "b\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 | |
include_once "./randomString.php"; | |
$s = []; | |
for ($x = 0; $x < 100000; $x++) { | |
$s[] = [ | |
"name" => randomString(), | |
"age" => rand(1, 100), | |
]; | |
} | |
echo "Arrays only peak memory usage: " . memory_get_peak_usage() . "b\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
#!/usr/bin/env bash | |
time php ./arr.php | |
time php ./loose-obj.php | |
time php ./obj.php |
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 | |
include_once "./randomString.php"; | |
$s = []; | |
class MyArrayObject { | |
public $name, $age; | |
public function __construct(string $name, string $age) { | |
$this->name = $name; | |
$this->age = $age; | |
} | |
} | |
for ($x = 0; $x < 100000; $x++) { | |
$s[] = new MyArrayObject(randomString(), rand(1, 100)); | |
} | |
echo "Objects only peak memory usage: " . memory_get_peak_usage() . "b\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 | |
function randomString($length = 10) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, $charactersLength - 1)]; | |
} | |
return $randomString; | |
} |
I've tested with another structure of class and it turn out it use even less memory.
Normally I will recast an array into an object because of readability like this:
$obj = (object) [
'key' => 'value'
];
but according to this test, this will consumes memory the most. anyway I don't like to repeat properties assignment inside every class so I made an abstract class with a constructor accepting an array as only parameter. then constructor will loop through parameter and assign the value
abstract class Main {
public function __construct( $properties = [] ) {
foreach( $properties as $property => $value ) {
if( property_exists( $this, $property ) ) {
$this->$property = $value;
}
}
}
}
class MyArrayObject extends Main {
public $name, $age;
}
for( $x = 0 ; $x < 100000 ; $x++ ) {
$s[] = new MyArrayObject([
'name' => randomString(),
'age' => rand(1, 100)
]);
}
from what I've tested, this use even less memory. (16.82MB vs 19.60MB) https://docs.google.com/spreadsheets/d/1jviu-peSDFMP4F5CmFm3TgN1Wlnh4QvaeTXEeAy9HiA/edit?usp=sharing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results after running
bench.sh
which does 100,000 iterations. Random strings are used for the values.