Yielding Bells πŸ”” – generators in PHP

Today we want us some bells in our Christmas, so let’s create them. PHP has a neat feature called generators. Generators are functions that can be used to create iterators, and they don’t need a large overhead to create them. For performance sake, let’…


This content originally appeared on DEV Community and was authored by Anders BjΓΆrkland

Today we want us some bells in our Christmas, so let's create them. PHP has a neat feature called generators. Generators are functions that can be used to create iterators, and they don't need a large overhead to create them. For performance sake, let's compare two code snippets.

Snippet 1:

<?php 

function produceBells(int $num)
{
    $bells = [];
    for ($i = 0; $i < $num; $i++) {
        $bells[] = 'πŸ””';
    }
    return $bells;
}
$hundredBells = produceBells(100); // 8.3KB

foreach ($hundredBells as $bell) {
    echo $bell . ' ';
}

Snippet 2:

function generateBells(int $num)
{
    for ($i = 0; $i < $num; $i++) {
        yield 'πŸ””';
    }
}
$hundredBells = generateBells(100); //544 bytes

foreach ($hundredBells as $bell) {
    echo $bell . ' ';
}

Both these snippets will produce this output:

πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ”” πŸ””

Is that a hundred bells? I sure hope so, I get lost when counting them!

The question is now, which is less memory intensive, and why is Snippet 2?! I ran both snippets with profile-mode on Xdebug and found the following result:

Snippet Type Memory usage
1 Array 8280 bytes
2 Generator 544 bytes

As we can see, the generator is a lot more memory efficient than the array. The reason for this is that generators are lazy, and only generate the values when they are needed. This means that the memory usage is only as high as the amount of values that are actually needed. And as we loop through one item at a time to echo it out, that is as much memory as will be used. You could output a million of these things if you like, and the memory usage would not be that much higher. (In fact, it remained at 544 bytes when I ran it.)

So what's the cost? Generators, while memory efficient, are not as time efficient. My machine is not stable enough (poor potato), to do a fair benchmarking. But at a thousand bells we got 9.2 ms for the array and 11.6 ms for the generator. So is it worth it for you to use generators? That question has not only to do with time versus memory cost. If you don't know how often you will need to access an array (or iterator) value, you may need to use a generator anyway.

What about you?

Have you ever tried a generator before? What use cases do you have or think you would have for generators? Comment below and let us know what you think ✍

Further Reading


This content originally appeared on DEV Community and was authored by Anders BjΓΆrkland


Print Share Comment Cite Upload Translate Updates
APA

Anders BjΓΆrkland | Sciencx (2021-12-18T08:51:30+00:00) Yielding Bells πŸ”” – generators in PHP. Retrieved from https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/

MLA
" » Yielding Bells πŸ”” – generators in PHP." Anders BjΓΆrkland | Sciencx - Saturday December 18, 2021, https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/
HARVARD
Anders BjΓΆrkland | Sciencx Saturday December 18, 2021 » Yielding Bells πŸ”” – generators in PHP., viewed ,<https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/>
VANCOUVER
Anders BjΓΆrkland | Sciencx - » Yielding Bells πŸ”” – generators in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/
CHICAGO
" » Yielding Bells πŸ”” – generators in PHP." Anders BjΓΆrkland | Sciencx - Accessed . https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/
IEEE
" » Yielding Bells πŸ”” – generators in PHP." Anders BjΓΆrkland | Sciencx [Online]. Available: https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/. [Accessed: ]
rf:citation
» Yielding Bells πŸ”” – generators in PHP | Anders BjΓΆrkland | Sciencx | https://www.scien.cx/2021/12/18/yielding-bells-%f0%9f%94%94-generators-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.