Skip to content

Instantly share code, notes, and snippets.

@charrondev
Last active March 26, 2022 11:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charrondev/be56ef53e0408b959eff6d2d911e8c1e to your computer and use it in GitHub Desktop.
Save charrondev/be56ef53e0408b959eff6d2d911e8c1e to your computer and use it in GitHub Desktop.
PHP Object vs Array memory usage with random values
<?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";
<?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";
#!/usr/bin/env bash
time php ./arr.php
time php ./loose-obj.php
time php ./obj.php
<?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";
<?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;
}
@charrondev
Copy link
Author

charrondev commented Nov 26, 2018

Results after running bench.sh which does 100,000 iterations. Random strings are used for the values.

Type Memory Usage Real execution Time
stdClass 51,232,368 0m0.156s
Arrays 46,191,568 0m0.137s
Classes 20,537,808 0m0.129s

@IronGhost63
Copy link

IronGhost63 commented May 13, 2019

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