[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Array comparison.. why does this work.

Daniel Sheppard

3/1/2007 5:33:00 AM

> I was curious if it just interates through one array looking for that
> element in the second, or does it (internally) sort the
> array, iterate
> each array performing comparision and if a > b skipping the equality
> determination to save CPU cycles and doing a 'next'.

I would guess that it would just do an include? call on the second array
and delete the element if it is true.

Doing the sort first would probably not be faster - a sort operation is
not a cheap operation, and there is no guarantee that the members of an
array are mutually sortable anyway (["a", 1, Object.new] for example) -
there is no way in which the '-' method would be able to rely on calling
the '<=>' method on its contents.

Dan.

2 Answers

Justin Collins

3/1/2007 6:03:00 AM

0

Daniel Sheppard wrote:
>> I was curious if it just interates through one array looking for that
>> element in the second, or does it (internally) sort the
>> array, iterate
>> each array performing comparision and if a > b skipping the equality
>> determination to save CPU cycles and doing a 'next'.
>>
>
> I would guess that it would just do an include? call on the second array
> and delete the element if it is true.
>
> Doing the sort first would probably not be faster - a sort operation is
> not a cheap operation, and there is no guarantee that the members of an
> array are mutually sortable anyway (["a", 1, Object.new] for example) -
> there is no way in which the '-' method would be able to rely on calling
> the '<=>' method on its contents.
>
> Dan.
>

Don't forget, you can always just look at the source[1].
It looks to me like it converts the second array to a hash, then
iterates through the first array, checking if each element is in the
newly created hash.
If it is, it skips it. If not, the value goes in the new array.

-Justin

[1] http://ruby-doc.org/core/classes/Array.src/M0...

Jean Nibee

3/1/2007 6:31:00 AM

0

>
> Don't forget, you can always just look at the source[1].
> It looks to me like it converts the second array to a hash, then
> iterates through the first array, checking if each element is in the
> newly created hash.
> If it is, it skips it. If not, the value goes in the new array.
>
> -Justin
>
> [1] http://ruby-doc.org/core/classes/Array.src/M0...

Awesome I didn't now where to find the sources to read them.

Thanks!

--
Posted via http://www.ruby-....