Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created May 30, 2022 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlackScorp/e8c439ea4337b4ebe9df9a59c96308d9 to your computer and use it in GitHub Desktop.
Save BlackScorp/e8c439ea4337b4ebe9df9a59c96308d9 to your computer and use it in GitHub Desktop.
Compare
<?php
require_once __DIR__.'/MyObject.php';
$time = microtime(true);
$memory = memory_get_usage();
$object = new MyObject();
for ($i = 0; $i < 1000000; $i++) {
$object = $object->setImmutableValue('value ' . $i);
}
$memory = memory_get_usage() - $memory;
$time = microtime(true) - $time;
echo "Memory usage: " . $memory . "<br/>";
echo "Time usage: " . $time . "<br/>";
<?php
require_once __DIR__.'/MyObject.php';
$time = microtime(true);
$memory = memory_get_usage();
$object = new MyObject();
for ($i = 0; $i < 1000000; $i++) {
$object->setValue('value ' . $i);
}
$memory = memory_get_usage() - $memory;
$time = microtime(true) - $time;
echo "Memory usage: " . $memory . "<br/>";
echo "Time usage: " . $time . "<br/>";
<?php
class MyObject
{
private string $value;
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
/**
* @param string $value
*/
public function setImmutableValue(string $value): self
{
$obj = clone $this;
$obj->value = $value;
return $obj;
}
/**
* @param string $value
*/
public function setValue(string $value): void
{
$this->value = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment