![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
|
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |