Below you can see two simplified fragments that do not change in their result.
Sample 1, objects from scratch:
foreach ($recipients as $recipient) { $message = new Message(); $message->setBody("This is the body of the message."); $message->setRecipient($recipient); $transport->sendMessage($message); $persister->saveToDatabase($message);
Sample 2, a clone of the prototype object:
$prototype = new Message(); $prototype->setBody("This is the body of the message."); foreach ($recipients as $recipient) { $message = clone $prototype; $message->setRecipient($recipient); $transport->sendMessage($message); $persister->saveToDatabase($message);
Does cloning objects (template 2) provide performance improvements when creating objects from scratch (template 1) in terms of memory usage, garbage collection and / or processor cycles? Consider also a large number of fixed properties (which do not change between instances) and a large number of cycles.
Update: I need different instances of objects in each loop. I added the saveToDatabase call to examples similar to this, let him, for example, provide an identifier for this message.;)
source share