Memory usage and performance improvements make everybody happier, from end-user to cloud and infrastructure engineers. And they are all right, and this is an optimization that we should try to achieve as much as possible.
I am also keeping this page for a reference to my future self because we cannot rely too much on our memory, and that will be a good reference I want to re-visit. I will make constant updates on this page. Let's have fun.
Arrays have a larger footprint to avoid constant memory pointers reassignments. It then reserves large amounts of memory when more elements or indexes are added.
Garbage collector is working as expected when the internal reference count (how may times a value is used) reaches zero:
$x = "foobar"; // refcount = 1 $y = $x; // refcount = 2 unset($x); // refcount = 1 unset($y); // refcount = 0 -> garbage collector will be happy ==> Destroy!
But self-referencing can be tricky:
$x = ; // refcount = 1 $x =& $x; // refcount = 2 unset($x); // refcount = 1 // It will never come to zero due to cycle
Cycle collector will eventually destroy it, but it will hang on memory for a while anyway.
PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates, but that is ok. This page was also created so that you would be able to find discovery in these statistics and then maybe re-run these tests in your own server environment to play around with this idea yourself, by using the code examples (these code examples are automatically generated and as the code in my .php files change, so do they).
Collection of tests and benchmarks for common operations in PHP. Tests run on several versions of PHP. There is an option to compare different solutions for the same problem to compare performances between them, such as checking values with