Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - efficient swap method

 
Thread Tools Search this Thread
Old 10-31-2009, 06:10 PM   #1
Default efficient swap method


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


forums_mp@hotmail.com
  Reply With Quote
Old 11-01-2009, 02:47 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: efficient swap method
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


Victor Bazarov
  Reply With Quote
Old 11-01-2009, 06:49 PM   #3
peter koch
 
Posts: n/a
Default Re: efficient swap method
On 1 Nov., 15:47, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> forums...@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.
>


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

/Peter


peter koch
  Reply With Quote
Old 11-01-2009, 11:21 PM   #4
forums_mp@hotmail.com
 
Posts: n/a
Default Re: efficient swap method
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.




forums_mp@hotmail.com
  Reply With Quote
Old 11-02-2009, 10:31 AM   #5
James Kanze
 
Posts: n/a
Default Re: efficient swap method
On Nov 1, 2:47 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> forums...@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.


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


James Kanze
  Reply With Quote
Old 11-02-2009, 02:16 PM   #6
Victor Bazarov
 
Posts: n/a
Default Re: efficient swap method
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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Victor Bazarov
  Reply With Quote
Old 11-02-2009, 02:26 PM   #7
Victor Bazarov
 
Posts: n/a
Default Re: efficient swap method
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?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Victor Bazarov
  Reply With Quote
Old 11-02-2009, 04:30 PM   #8
forums_mp@hotmail.com
 
Posts: n/a
Default Re: efficient swap method
On Nov 2, 9:16*am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

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




forums_mp@hotmail.com
  Reply With Quote
Old 11-02-2009, 04:43 PM   #9
Victor Bazarov
 
Posts: n/a
Default Re: efficient swap method
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.

> [..]



Victor Bazarov
  Reply With Quote
Old 11-03-2009, 09:21 AM   #10
James Kanze
 
Posts: n/a
Default Re: efficient swap method
On Nov 2, 2:26 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> 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?


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


James Kanze
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cancelling Timer and TimerTask threads wfalby Software 1 04-30-2009 01:04 PM
Right Line Traders v 2006, Technical Analysis programs 2006-2004 of stocks/commodities/futures markets, other Financial software atarax Computer Support 0 12-11-2006 01:19 PM
Technical Analysis programs 2006-2004 of stocks/commodities/futures markets, other Financial software kashumoto_tokugawa Computer Information 0 11-07-2006 04:33 PM
Although the Whirlpool Duet has better water efficiency, the Frigidaire Gallery is less expensive and cleans nearly as well, reviewers say. Experts like front-loaders Dobey House Elf Computer Security 3 07-21-2004 09:25 PM
Win Rar Problem Andy Computer Support 3 06-15-2004 04:01 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46