[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: How to pass arguments by reference in a function

seebs

7/27/2007 7:16:00 PM

In message <0514e8b08bc1a44593d7ffe0ddb5117e@ruby-forum.com>, Lloyd Linklater w
rites:
>That reminds me, in the old days you could do a swap without a third
>variable using XOR. e.g.
>
>a = 13
>b = 17
>
>a ^= b
>b ^= a
>a ^= b
>
>and they are swapped.
>
>I know that Ruby might do it as mentioned above.
>
>My question is, which is more efficient?

The xor swap is virtually never more efficient on reasonable hardware.
A casual pass through suggests that, even for the special case of numbers
where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be
noticably faster.

More importantly: How could doing it that way ever be idiomatic or easy
to understand? I cannot conceive of a program where I have to swap two
objects quickly, and the need to swap them quickly is so great that a hack
like that would be worth it... and I can't just bypass the swap by improving
the algorithm.

-s

2 Answers

Bill Kelly

7/27/2007 7:36:00 PM

0


From: "Peter Seebach" <seebs@seebs.net>
>
> The xor swap is virtually never more efficient on reasonable hardware.
> A casual pass through suggests that, even for the special case of numbers
> where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be
> noticably faster.
>
> More importantly: How could doing it that way ever be idiomatic or easy
> to understand? I cannot conceive of a program where I have to swap two
> objects quickly, and the need to swap them quickly is so great that a hack
> like that would be worth it... and I can't just bypass the swap by improving
> the algorithm.

Incidentally, I do recall one interesting use of this XOR swap
technique.

I believe this is how GUI menus were drawn on the screen on the
Amiga. When you'd click the mouse to get a menu, the system
would allocate off-screen bitmap memory, and render the menu
into the off-screen bitmap. Then it would XOR-swap the off-screen
rendered menu, with the location on the screen where the menu
would be displayed. Thus preserving the pixels from the screen
that would be obscured by the menu, so that the pixels could
be restored when the menu was dismissed.

Pretty neat trick, since it avoided allocating an extra buffer
to store the obscured screen pixels. (And it was fast, because
the Amiga bitmap blitter supported logical raster op's including
XOR, etc.)


Regards,

Bill



Lionel Bouton

7/27/2007 8:05:00 PM

0

Peter Seebach wrote the following on 27.07.2007 21:16 :
> The xor swap is virtually never more efficient on reasonable hardware.
> A casual pass through suggests that, even for the special case of numbers
> where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be
> noticably faster.
>
> More importantly: How could doing it that way ever be idiomatic or easy
> to understand? I cannot conceive of a program where I have to swap two
> objects quickly, and the need to swap them quickly is so great that a hack
> like that would be worth it... and I can't just bypass the swap by improving
> the algorithm.
>

A program in assembler for a microcontroller with so few memory that
using the stack for a temporary variable is out of the question comes to
my mind.

But for sure no program written in Ruby would need this kind of hack.
Even more, if my assumptions of how Ruby internals work are correct,
using the xor in this case is less memory efficient as it should
generate new Fixnum objects instead of just swapping references to them...

Lionel.