Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: efficient swap method

2 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
Nov 1, 2009, 9:47:57 AM11/1/09
to
foru...@hotmail.com wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

peter koch

unread,
Nov 1, 2009, 1:49:40 PM11/1/09
to
On 1 Nov., 15:47, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

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

/Peter

foru...@hotmail.com

unread,
Nov 1, 2009, 6:21:55 PM11/1/09
to
On Nov 1, 9:47 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> 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.

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.


James Kanze

unread,
Nov 2, 2009, 5:31:25 AM11/2/09
to
On Nov 1, 2:47 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

> Not really.

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.

--
James Kanze

Victor Bazarov

unread,
Nov 2, 2009, 9:16:40 AM11/2/09
to
foru...@hotmail.com wrote:
> On Nov 1, 9:47 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>>> 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.
>
> 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

Victor Bazarov

unread,
Nov 2, 2009, 9:26:45 AM11/2/09
to
James Kanze wrote:
> On Nov 1, 2:47 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> [..]

>> 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?

Message has been deleted

Victor Bazarov

unread,
Nov 2, 2009, 11:43:03 AM11/2/09
to
foru...@hotmail.com wrote:
> [..]
> 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.

"Game on"? <chuckle> Apparently you got nothing better to do. Oh well.

> [..]

James Kanze

unread,
Nov 3, 2009, 4:21:17 AM11/3/09
to

> > [..]

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.

--
James Kanze

0 new messages