marc-mabe / php-enum

Simple and fast implementation of enumerations with native PHP
BSD 3-Clause "New" or "Revised" License
464 stars 36 forks source link

use of generators #93

Closed marc-mabe closed 7 years ago

marc-mabe commented 7 years ago

@prolic please can you have a look - I just tested if it makes sense to use generators but this performance speed-up must be wrong but all tests passed:

benchmark: EnumBench, subject: benchGetEnumerators
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2500 | 11.202μs               | 11.211μs                   |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet32Bench, subject: benchGetEnumerators
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 15.575μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet66Bench, subject: benchGetEnumerators
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 33.223μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumMapBench, subject: benchGetKeysEmpty
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 0.420μs                | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumMapBench, subject: benchGetKeysFull
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 22.028μs               | 0.060μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumBench, subject: benchGetNames
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2500 | 0.156μs                | 0.156μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet32Bench, subject: benchGetNames
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 19.462μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet66Bench, subject: benchGetNames
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 40.846μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumBench, subject: benchGetOrdinals
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2500 | 0.519μs                | 0.505μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet32Bench, subject: benchGetOrdinalsEmpty
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 1.114μs                | 0.295μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet66Bench, subject: benchGetOrdinalsEmpty
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 0.548μs                | 0.279μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet32Bench, subject: benchGetOrdinalsFull
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 1.645μs                | 0.295μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet66Bench, subject: benchGetOrdinalsFull
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 4.801μs                | 0.278μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumBench, subject: benchGetValues
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2500 | 0.428μs                | 0.428μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet32Bench, subject: benchGetValues
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 16.630μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumSet66Bench, subject: benchGetValues
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 35.573μs               | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumMapBench, subject: benchGetValuesEmpty
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 0.128μs                | 0.059μs                    |
+--------+--------+------+------------------------+----------------------------+

benchmark: EnumMapBench, subject: benchGetValuesFull
+--------+--------+------+------------------------+----------------------------+
| groups | params | revs | vcs_branch:master:mean | vcs_branch:generators:mean |
+--------+--------+------+------------------------+----------------------------+
|        | []     | 2000 | 0.328μs                | 0.060μs                    |
+--------+--------+------+------------------------+----------------------------+
prolic commented 7 years ago

The change does look good to me. The performance difference is obvious, the generator version returns an iterator, that did nothing yet, you have to loop through all the values in order to see a difference in performance.

prolic commented 7 years ago

I suggest you loop through all the values in your benchmark, and then we compare again, but I assume generators would use less memory and take more time.

marc-mabe commented 7 years ago

... the generator version returns an iterator, that did nothing yet ...

Yea you are totally right - thx

marc-mabe commented 7 years ago

Now tried it by iterate over the items in bench.

The result is exactly as you suggested: the generator is a bit slower in all cases (except empty list) but needs very little less memory. -> As such lists are limited by the number of defined enumerators anyway there is no need for generators.

But it was a try ;)