I have the following two code. Which code is good in terms of performance?
$gd = $this->getDoctrine();
$em = $gd->getEntityManager();
$data = $gd->getRepository('MyB:MyC')->find...;
// update $data.
$em->persist($data);
$em->flush();
I want to know its memory usage and execution speed.
$data = $this->getDoctrine()->getRepository('MyB:MyC')->find...;
// update $data.
$this->getDoctrine()->getEntityManager()->persist($data);
$this->getDoctrine()->getEntityManager()->flush();
Short answer: The former one will be faster is most cases, but eats up more memory.
tl;dr
Depends on how the underlying
getDoctrine()works, the second method will at least add some method-calling overheads in execution.If the methods do calculations and do not have any caching mechanism, you will also have to trace down the whole call stack for this.
There is always a tradeoff between memory footprint and performance overhead.
EDIT
Worse case: The latter will create an instance of some kind of underlying data, every single time you call a getter. This do not help in performance, even creates heap spikes.
Best case: All subsequence call to the getters are accessing the same object, which is in fact quite rare in DAO or SQL helpers, consumes the exact same memory as the first one just because there is a reference kept inside the parent object.
My suggestion in this case, use the first one.