lstrojny / functional-php

Primitives for functional programming in PHP
MIT License
1.98k stars 204 forks source link

Flatten array performance #168

Closed drupol closed 6 years ago

drupol commented 6 years ago

Hello,

I've run a benchmark on Functional\flatten(). I wanted to compare it to the function I'm using to flatten an array.

Find the benchmark here: https://3v4l.org/cXDe6

As I can see, the first method (flatten1()) is a bit faster in almost every cases.

Let me know if you would be interested to have a PR and include it.

Thanks!

phanan commented 6 years ago

Won't flatten1() break if any of the inputs is a Traversable, which is correctly handled by the current implementation?

drupol commented 6 years ago

I will check.

drupol commented 6 years ago

You're perfectly right, see the example: https://3v4l.org/3db8j

I guess I will close this issue :-)

drupol commented 6 years ago

I've updated the example, it now works with a Traversable as well... and it's most of the time a bit faster.

Link: https://3v4l.org/VFV2h

phanan commented 6 years ago

I've updated the example, it now works with a Traversable as well... and it's most of the time a bit faster.

Except that you're incorrectly typecasting $input into array instead of using the proper iterator_to_array() function. Anyway "most of the time a bit faster" doesn't really justify a PR IMHO :)

drupol commented 6 years ago

That's true :-) Thanks for the tips.