Skip to content

Instantly share code, notes, and snippets.

@sokil
Created April 1, 2015 10:16
Show Gist options
  • Save sokil/87a0e3d7f65a4f2e7c4b to your computer and use it in GitHub Desktop.
Save sokil/87a0e3d7f65a4f2e7c4b to your computer and use it in GitHub Desktop.
<?php
interface IMessage
{
public function getBody();
}
class Message implements IMessage
{
private $body;
public function __construct($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
public function setBody($body)
{
$this->body = $body;
}
}
class Sender
{
public function sendArray(array $message)
{
return $message['body'];
}
public function sendMessage(IMessage $message)
{
return $message->getBody();
}
}
$sender = new Sender();
$iterationCount = 1000000;
if($argv[1] == 1) {
// test cloned class
$t = microtime(true);
$message = new Message(null);
for($i = 0; $i < $iterationCount; $i++) {
$messageClone = clone $message;
$messageClone->setBody('hello');
$sender->sendMessage($messageClone);
}
$st = microtime(true) - $t;
echo 'Cloned class: ' . $st . PHP_EOL;
}
if($argv[1] == 2) {
// test class
$t = microtime(true);
for($i = 0; $i < $iterationCount; $i++) {
$sender->sendMessage(new Message('hello'));
}
$st = microtime(true) - $t;
echo 'Class: ' . $st . PHP_EOL;
}
// test array
if($argv[1] == 3) {
$t = microtime(true);
for($i = 0; $i < $iterationCount; $i++) {
$sender->sendArray(['body' => 'hello']);
}
$st = microtime(true) - $t;
echo 'Array: ' . $st . PHP_EOL;
}
@sokil
Copy link
Author

sokil commented Apr 1, 2015

Cloned class: 2.5899231433868
Class: 2.5238420963287
Array: 1.0065770149231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment