efficient swap method

F

forums_mp

Consider:
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
// assert macro
union {
T value;
char bytes[ size ];
} input, output;
input.value = value;
for ( unsigned int idx = 0; idx < size / 2; ++idx )
{
output.bytes[ idx ] = input.bytes[size - 1 - idx ];
output.bytes[size - 1 - idx ] = input.bytes[ idx ];
}
return output.value;
}

Interested in perhaps a more efficient means to achieve the same
objective? Benchmarks over a million interations shows the funciton
above as approximately ~55% of my total number. std::swap if memory
serves is linear time so .. i'm wondering (laptop got fried last night
so I'm unable to check) if a standard swap approach would eb more
prudent
 
V

Victor Bazarov

Consider:
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
// assert macro
union {
T value;
char bytes[ size ];
} input, output;
input.value = value;
for ( unsigned int idx = 0; idx < size / 2; ++idx )
{
output.bytes[ idx ] = input.bytes[size - 1 - idx ];
output.bytes[size - 1 - idx ] = input.bytes[ idx ];
}
return output.value;
}

Interested in perhaps a more efficient means to achieve the same
objective?

Not really.

Oh.. That wasn't a question, was it?
> Benchmarks over a million interations shows the funciton
above as approximately ~55% of my total number. std::swap if memory
serves is linear time so .. i'm wondering (laptop got fried last night
so I'm unable to check) if a standard swap approach would eb more
prudent

Why TF do you ask when it's possible only for you to answer - by trying
and measuring? Seriously, dude, use 'std::swap' and see.

The only two other comments: if your T has the same alignment
requirements as the CPU word, you could swap words instead of bytes and
get a bit of improvement that way. Another possible improvement is (a)
to pass by reference and avoid all the copying and (b) to use
reinterpret_cast (provided that it gives the correct answer on your
platform) and forgo internal copying to 'input'. You're in the
undefined behaviour land anyway with your use of unions.

V
 
P

peter koch

Consider:
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
  // assert macro
  union  {
    T value;
    char bytes[ size ];
  } input, output;
  input.value = value;
  for ( unsigned int idx = 0; idx < size / 2; ++idx )
  {
    output.bytes[ idx ] = input.bytes[size - 1 - idx ];
    output.bytes[size - 1 - idx ] = input.bytes[ idx ];
  }
  return output.value;
}
Interested in perhaps a more efficient means to achieve the same
objective?

Not really.

Oh.. That wasn't a question, was it?

 >  Benchmarks over a million interations shows the funciton
above as approximately ~55% of my total number.   std::swap if memory
serves is linear time so .. i'm wondering (laptop got fried last night
so I'm unable to check) if a standard swap  approach would eb more
prudent

Why TF do you ask when it's possible only for you to answer - by trying
and measuring?  Seriously, dude, use 'std::swap' and see.

The only two other comments: if your T has the same alignment
requirements as the CPU word, you could swap words instead of bytes and
get a bit of improvement that way.  Another possible improvement is (a)
to pass by reference and avoid all the copying and (b) to use
reinterpret_cast (provided that it gives the correct answer on your
platform) and forgo internal copying to 'input'.  You're in the
undefined behaviour land anyway with your use of unions.

Also, bitswapping is likely to not work on lots of types.

/Peter
 
F

forums_mp

Why TF do you ask when it's possible only for you to answer - by trying
and measuring?  Seriously, dude, use 'std::swap' and see.

Frankly it would be prudent for you tell me how you want me to work
with you. You see, I have no issues adapting and at this juncture do
me a favor and go F yourself. For years, I've watched your response on
this board and frankly I'm one who is tired of your nonsense. I made
it clear my laptop is fried and as such I'm using borrowed goods. Do
me a favor.... Stay clear of my F(ing) posts. Thank you.
 
J

James Kanze

Consider:
template < typename T, unsigned int size>
inline T swap_bytes ( T const value ) {
// assert macro
union {
T value;
char bytes[ size ];
} input, output;
input.value = value;
for ( unsigned int idx = 0; idx < size / 2; ++idx )
{
output.bytes[ idx ] = input.bytes[size - 1 - idx ];
output.bytes[size - 1 - idx ] = input.bytes[ idx ];
}
return output.value;
}
Interested in perhaps a more efficient means to achieve the
same objective?
Not really.
Oh.. That wasn't a question, was it?
Why TF do you ask when it's possible only for you to answer -
by trying and measuring? Seriously, dude, use 'std::swap' and
see.

His function doesn't do the same thing as std::swap. It looks
more like a value oriented version of std::reverse, i.e.
something like:

template< typename T, unsigned int size >
T reverse_bytes( T value )
{
std::reverse( reinterpret_cast< char* >( &value ),
reinterpret_cast< char* >( &value ) + size);
return value;
}

But the interface still looks a bit screwy: you have to
explicitly state each template argument. And it's not really
applicable except to arrays of char, since anything else will
result in undefined behavior, and a mess. And if T is an array
type, you can't pass or return it by value. And if it is a
container, you don't need the size argument; I could see some
argument for a function:

template< typename Container >
Container
reverse( Container c )
{
std::reverse( c.begin(), c.end() );
return c;
}

But using std::reverse_copy is likely to be faster most, if not
all of the time.
 
V

Victor Bazarov

Frankly it would be prudent for you tell me how you want me to work
with you.

I don't. I don't know you: you have no name. I don't work with people
with no name. To me you're just a poster who asked a question. I don't
care to "work" with somebody who doesn't want to identify him-/herself.
> You see, I have no issues adapting and at this juncture do
me a favor and go F yourself. For years, I've watched your response on
this board and frankly I'm one who is tired of your nonsense.

For years you watched? I'm flattered... No, I guess I am not. I don't
care, really. Oh, did I offend you? I apologise. Only I don't know to
whom I just apologised. Because I have no idea who you are - you don't
sign your posts.
> I made
it clear my laptop is fried and as such I'm using borrowed goods. Do
me a favor.... Stay clear of my F(ing) posts. Thank you.

Sorry, no can do, unless you start top-posting or s-top posting (pun
intended). Why should I care that your laptop is fried? My potatoes
are fried, and it makes no difference to you, does it? You asked a
question that cannot be answered. Go find yourself something else to do
if you can't take my replies. Killfile me for all I care. Read
http://www.catb.org/~esr/faqs/smart-questions.html

V
 
V

Victor Bazarov

James said:
Why TF do you ask when it's possible only for you to answer -
by trying and measuring? Seriously, dude, use 'std::swap' and
see.

His function doesn't do the same thing as std::swap. It looks
more like a value oriented version of std::reverse, i.e.
something like:

template< typename T, unsigned int size >
T reverse_bytes( T value )
{
std::reverse( reinterpret_cast< char* >( &value ),
reinterpret_cast< char* >( &value ) + size);
return value;
}

[..]

My point is that it's useless to *ask* whether performance of one method
is better than some other method (unless they are clearly different only
from that particular point of view), and instead the performance has to
be *compared*, in real life, on a real system, once both methods are
correctly implemented. That's all. I don't want to pretend I know what
the OP meant about "prudence" of "standard swap approach", I've just
assumed the OP knows what they mean.

Does this explain my point?

V
 
F

forums_mp

Sorry, no can do, unless you start top-posting or s-top posting (pun
intended).  Why should I care that your laptop is fried?  My potatoes
are fried, and it makes no difference to you, does it?  You asked a
question that cannot be answered.  Go find yourself something else to do
if you can't take my replies.  Killfile me for all I care.  Readhttp://www.catb.org/~esr/faqs/smart-questions.html

V

No I don't have to find something else to do if "(I) can't take (your)
replies".

My message to you is simple: It's game on when you opt to drop to F
bombs when dealing with a response to my post. I don't have to/wont
put up with your s/h/i/t, that's a guarantee.

Indeed, the fact that I don't have a compiler (hence the dead laptop)
is non of your concern, more importantly results from my initial
implementation compared to the one below showed the initial
implementation as 'slow'.

template < typename T >
inline T SwapBytes ( T const value );

template <>
inline unsigned short SwapBytes ( unsigned short const value ) {
return ( ( value & 0x00FF ) | ( value & 0xFF00 ) ) ;
}

So I thought, improve the initial implementation by using std::swap or
provide a bunch of specializations. In any event, peruse Kanze's
reply, that's how you address a post without resorting to F bombs.
 
J

James Kanze

James said:
Why TF do you ask when it's possible only for you to answer
- by trying and measuring? Seriously, dude, use
'std::swap' and see.
His function doesn't do the same thing as std::swap. It
looks more like a value oriented version of std::reverse,
i.e. something like:
template< typename T, unsigned int size >
T reverse_bytes( T value )
{
std::reverse( reinterpret_cast< char* >( &value ),
reinterpret_cast< char* >( &value ) + size);
return value;
}
[..]
My point is that it's useless to *ask* whether performance of
one method is better than some other method (unless they are
clearly different only from that particular point of view),
and instead the performance has to be *compared*, in real
life, on a real system, once both methods are correctly
implemented. That's all. I don't want to pretend I know what
the OP meant about "prudence" of "standard swap approach",
I've just assumed the OP knows what they mean.
Does this explain my point?

I wasn't disagreeing with you in that regard. Just pointing out
that it makes no sense to compare his function with std::swap,
because they do radically different things. One step even
further back, so to speak.

And given the code and the discussion, I think it's probably
safe to say that the OP doesn't really know what they mean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top