Created
May 30, 2022 15:42
-
-
Save BlackScorp/e8c439ea4337b4ebe9df9a59c96308d9 to your computer and use it in GitHub Desktop.
Compare
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 | |
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/>"; |
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 | |
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/>"; |
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 | |
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