Yukihiro M. wrote:
In message “Re: Why does Array#first not use rb_ary_subseq?”
on Fri, 18 May 2007 21:36:19 +0900, Stefan R.
[email protected] writes:
|When I run a small bench I noticed that Array#first(n) with an argument
|is vastly slower than Array#[0,n].
The difference is due to Array#first is optimized for memory
consumption, I guess. We’d like profile on the use-case, so could you
show us “vastly slower” example code?
Thank you for your reply.
Memory consumption: could you please elaborate that a bit for me?
I somehow expected the COW Array created by Array# to be more memory
efficient as I understood that to be the reason why that is used at all.
I’m new to C, so please forgive me if I’m a bit slow
I posted a benchmark with results and code on
http://pastie.caboo.se/62638. I don’t have a specific use-case but I’d
say a lot of array operations base on .each (all Enumerable methods)
which is why I added a bench running an each loop over the elements.
It’s intriguing that the difference only seems to be huge for the
creation. In my initial bench I only run the  and first method, I
didn’t add an each-loop. I didn’t expect this as the returned array is
not modified. I think I have to delve a bit more into the whys and whats
with that COW array.
But what I forgot to ask in my initial question: if those two
implementations have different characteristics, why isn’t that
documented? (from the docs I’d just expect first to have nicer semantics
for its use-case and nothing else)