Archived

This topic is now archived and is closed to further replies.

farmersckn

Swap macro

Recommended Posts

I saw one that didn''t use a temp variable, it xored the variables or something, is anyone familiar with this? if you can remember how it was written, i''d appreciate it. thanks

Share this post


Link to post
Share on other sites
because of the problem, that you couldnt use binary operators onto floats, you could use the macro like this:

#define SWAP(a,b) {(*((int*)&a))^=(*((int*)&b));(*((int*)&b))^=(*((int*)&a));(*((int*)&a))^=(*((int*)&b));}

working for floats, too (it just reads the floats like ints, thats all, but its working, and thats what we want)

we wanna play, not watch the pictures

Share this post


Link to post
Share on other sites
quote:
Original post by goir

Please use temp variables, expressions like that make my brain start to boil


Agreed. And davepermen''s is even more of a brain boiler. Wasn''t there a xchange assembler command (swap two 32 bit values)? I''m sure I read about it somewhere and if we get away from macros like this...


Share this post


Link to post
Share on other sites
how could that float version of swap possibly be worth it? six type conversions, and alot of pointer dereferences..

------------------------
IUnknown *pUnkOuter

"Just do your best and
don't worry"
--Morrissey

"Try the best you can
try the best you can
the best you can is good enough"
--Radiohead

"Are you looking for new ways
to do better than your worst"
--Nick Drake

Share this post


Link to post
Share on other sites
it works, that was all what I wannted to show, but, yep, its stupid, and a inline like that is bether

inline void swap(int& x,int& y)
{
int buffer=x;
x=y;
y=buffer;
}

because you could even use it for big classes (just overload it)

we wanna play, not watch the pictures

Share this post


Link to post
Share on other sites
quote:

Why not something like this:



Because std::swap already does this, and it''s specialised for some things (e.g. std::vector)

Share this post


Link to post
Share on other sites
BTW, just thought I'd mention the performance of the two ways mentioned to swap variables:


void Func1()
{
static int a = 1;
static int b = 2;

a^=b^=a^=b;
}


void Func2()
{
static int a = 1;
static int b = 2;

int t = a;
a = b;
b = t;
}


Here's the time it took to run each function 100,000,000 times:
Function 1: 1.868420 seconds
Function 2: 1.264502 seconds

Not only is using a temporary value slightly faster, but it's also much easier to read.

- Houdini


Edited by - Houdini on October 8, 2000 10:07:25 PM

Share this post


Link to post
Share on other sites
I have to say, I hadn''t expected so many replies so fast, thanks. And, it seems, I didn''t *need* them, because the method i was looking for works slower. I''d already thought of and written the template swap function, but i thought the macro would be faster. thanks for proving me wrong. otherwise, i would have lost that .6% speed increase...
farmersckn

Share this post


Link to post
Share on other sites
quote:

I''d already thought of and written the template swap function, but i thought the macro would be faster.



If you''re saying you thought the macro that involved the temp would be faster (not the XOR one), that''s not true. If you make the template function inline, then it would be the same speed. Templates only require extra work at compile time to be used. That is, there is no run time performance hit. All the templates do is to compile an instance of the function for every type that your code uses it with.

Share this post


Link to post
Share on other sites