PHP's unset () function can improve performance?

7

I think the answer to my question would be "yes.", why I end up doing it in my code (when I remember), but I just stopped to think about it now and somehow I'm worried if someone I would not know how to answer the "why" of it satisfactorily.

Here is an example snippet:

<?php
# aqui um array multidimensional com 10k de linhas
$data  = array(...);
# aqui peguei o que eu precisava da variável acima
$total = (int) count($data); 
# a partir de agora não preciso mais usar a variável $data
?>

From the line that I no longer need to use a variable in the rest of the code, do I need to delete it using unset($data) as in what I mentioned above? Does that bring any improvement in performance in fact? Because I think the same processing I would have to load it myself without using it would be processing to delete it.

    
asked by anonymous 30.07.2016 / 21:36

4 answers

8

It depends on the context because if it is misapplied, it causes the opposite effect, increasing the use of memory and processing.

See a simple test:

Test 1

Here we use unset() to remove the indexes of the variable $arr 1 by 1.
The logic here is that we are already doing an iteration in each one of the indices so theoretically it could be more performative already to remove each one, right?

$arr = range(1, 100000);
foreach ($arr as $k => $v) {
    if ($k % 2 === 0) {
        $arr2[] = $v;
    }
    unset($arr[$k]);
}

The result of memory usage:

Pico : 17174288 (17mb)
Current : 10883880 (10mb)

Runtime (microseconds)

Total : 0.0090339183807373
Initial : 1469910070.9007
Final : 1469910070.9097

Test 2

In this second test, we think differently. Based on the logic that every time unset() is invoked, it would be consuming more memory and processing. As the end goal is to completely delete the variable $arr then we can do this by invoking unset () only once after the loop repetition.

$arr = range(1, 100000);
foreach ($arr as $k => $v) {
    if ($k % 2 === 0) {
        $arr2[] = $v;
    }
}
unset($arr);

The result of memory usage:

Pico : 10882752 (10mb)
Current : 4592344 (4mb)

Runtime (microseconds)

Total : 0.0061979293823242 Initial : 1469910142.5007 Final : 1469910142.5069

Here we draw a conclusion that the theory of the second test is correct for the context presented here. Repeated invocation of the unset() function, within the context of the test, causes the opposite effect, impairing performance significantly.

The runtime presents an "insignificant" difference of 0.003 micro seconds for human perception, however, it is a relevant difference in terms of data processing. But what most calls attention is the consumption of memory. The unset() function can improve performance by releasing meaningful memory space when used properly.

General rule:

  • Repeated invocation of a function generates greater consumption.
  • Apply unset() to objects that will no longer be used, especially if there are still more routines to execute. This will save memory for the rest of the processes.
  • Garbage Collector?

    We can argue that it is "unnecessary" to be careful because at the end of all processes everything "dies", but depending on context it is always good to free up space in memory.

    PHP has a memory usage limiter, defined in the settings (php.ini). Usually the default is between 64mb and 128mb. A script that comes in half of the processing already consuming 125mb, for example, has a great risk of causing interruption of execution due to lack of memory. Many of these out of memory cases could be avoided if the script cleans up variables that are not being used. Therefore, it is very valid to be careful in freeing the space in the memory during the executions.

    Terms used
    peak: greater amount of space used during execution, ie "peak memory"

    current: amount of memory being used right after unset ()

    start and end: the start time and end time of the execution in timestamp format, using the microtime ()

    total: it is the calculation of the difference between the initial time and the final time, in microseconds.

        
    30.07.2016 / 22:46
    7

    In fact, it's highly unlikely that you'll get some performance gain.

    This command says that the variable will no longer be used. This does not increase performance. It even consumes some minimal to say that.

    One might think that there will be a gain because the memory is released. This might even be true under very specific circumstances where it has a large memory constraint and the application has been running for a long time, which is rare in PHP. But it is not because the function even frees the memory. At most it allows the release to be made.

    So it would even be true in some case, but it would need to analyze it concretely with real code and structure it will run.

    If you find a situation where performance is important and there is a clear indication (you have to measure) that memory consumption is causing the code to

    30.07.2016 / 21:52
    1

    Hello,

    Your amount of free memory will have no effect on the performance of your programs in PHP. You will only have more space to allocate memory by creating more variables.

    Even in a situation where you consume all available memory, this would not have an impact on performance since the PHP interpreter limits the amount of memory that can be consumed by a process by preventing it from degrading the system. p>     

    30.07.2016 / 21:55
    1

    The unset() constructor does not have much effect or impact on performance per se, but can improve performance over a process that is being used later, such as session usage if you think hard global, you will be eliminating data in the process, which would eliminate resources and memory, however, will be executing a new process for this that will use memory, but very little, nothing that will impact effectively in the process. Unless it's misused as Daniel explains.

        
    01.08.2016 / 16:20