Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 18, 2021 08:51 am GMT

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'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.3KBforeach ($hundredBells as $bell) {    echo $bell . ' ';}

Snippet 2:

function generateBells(int $num){    for ($i = 0; $i < $num; $i++) {        yield '';    }}$hundredBells = generateBells(100); //544 bytesforeach ($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:

SnippetTypeMemory usage
1Array8280 bytes
2Generator544 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


Original Link: https://dev.to/andersbjorkland/yielding-bells-generators-in-php-2521

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To