Optimal swapping now that is clever
Note 1:
calling a funtion takes one clock cycle so use _inline to embed the code into your function!
Note 2:
calling a function in a function takes 2 cycles plus what the function does and if it''s a windows function it has lot''s of error handling which is sloowwww!
Note 3:
The machine code function "xchg" is processor intensive only use when trying to save memory space not for speed!
Note 4:
The pentium has a duel pipeline archtecture so it can do pairs of operations, so if you are clever with your code you can do this in two cycles!
So my code would look like this
void _inline swap(unsigned long& a, unsigned long& b)
{
_asm{
mov eax, mov ebx, //these instructions pair one cycle
mov [a], ebx
mov , eax //these instructions pair one cycle<br> }<br>} </b> '' Target=_Blank>Link</a>
Is there a way to swap two variables w/o a temp variable?
If you aren''t worried about overflow, then you can always do
Try it
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
a = a+b;b = a-b;a = a-b;
Try it
-Chris Bennett ("Insanity" of Dwarfsoft)
Check our site:
http://www.crosswinds.net/~dwarfsoft/
Check out our NPC AI Mailing List :
http://www.egroups.com/group/NPCAI/
made due to popular demand here at GDNet :)
Optimal swapping now that is clever
Note 1:
calling a funtion takes one clock cycle so use _inline to embed the code into your function!
Note 2:
calling a function in a function takes 2 cycles plus what the function does and if it's a windows function it has lot's of error handling which is sloowwww!
Note 3:
The machine code function "xchg" is processor intensive only use when trying to save memory space not for speed!
Note 4:
The pentium has a duel pipeline archtecture so it can do pairs of operations, so if you are clever with your code you can do this in two cycles!
So my code would look like this
void _inline swap(unsigned long& a, unsigned long& b)
{
_asm{
mov eax, [ a ] //These instructions
mov ebx, //pair one cycle<br><br> mov [ a ], ebx //These instorcutions<br> mov , eax //pair one cycle<br> }<br>}<br><br>Edited by - pcstiby on August 1, 2000 7:55:34 AM
Note 1:
calling a funtion takes one clock cycle so use _inline to embed the code into your function!
Note 2:
calling a function in a function takes 2 cycles plus what the function does and if it's a windows function it has lot's of error handling which is sloowwww!
Note 3:
The machine code function "xchg" is processor intensive only use when trying to save memory space not for speed!
Note 4:
The pentium has a duel pipeline archtecture so it can do pairs of operations, so if you are clever with your code you can do this in two cycles!
So my code would look like this
void _inline swap(unsigned long& a, unsigned long& b)
{
_asm{
mov eax, [ a ] //These instructions
mov ebx, //pair one cycle<br><br> mov [ a ], ebx //These instorcutions<br> mov , eax //pair one cycle<br> }<br>}<br><br>Edited by - pcstiby on August 1, 2000 7:55:34 AM
I program therefore can''t spell!
August 01, 2000 07:22 AM
#define SWAP(A, B) ((A)^=(B)^=(A)^=(B))
// or
template
inline void swap(T a, T b)
{
a^=b^=a^=b;
}
// or
template
inline void swap(T a, T b)
{
a^=b^=a^=b;
}
August 01, 2000 08:04 AM
grr... html syntax error. Hope this works.
template <class T>
inline void swap(T a, T b)
{
a^=b^=a^=b;
}
template <class T>
inline void swap(T a, T b)
{
a^=b^=a^=b;
}
How about this one:
template <class T>inline void _swap(T& a, T& b){ T t = a; a = b; b = t;}
That uses a temp. variable. We try to avoid to use one...
- - - - - - - - - - - - - -
Andreas Mähler
Wolf359 Interactive
- - - - - - - - - - - - - -
Andreas Mähler
Wolf359 Interactive
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement