Which one is faster (and why). Returning structure:or returning unsigned int. Lets assume that they both take 16bits. Which one will be faster?Code:struct { unsigned char foo1; unsigned char foo2; }
Which one is faster (and why). Returning structure:or returning unsigned int. Lets assume that they both take 16bits. Which one will be faster?Code:struct { unsigned char foo1; unsigned char foo2; }
Well, returning the structure could never be faster, but it may well be *just as fast*, depending on how the compiler decides to do things. At any rate, your structure is not guaranteed to be 16 bits wide due to potential padding. You'll need to use a compiler-specific directive. Under GCC, you could do something like:
Code:#pragma pack(push, 2) struct bar { unsigned char foo1; unsigned char foo2; }; #pragma pack(pop)
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Maybe I'll tell you what I want to do. I want to design a system return type. By that I mean a data type that would be used in the system (in some of the system functions). I came up with this structure soultion as I thought that one filed wold contain module type and the oder result for that module. If I go for unsigned int the I would have to use bit masking to strip the message.
Or maybe there is other solution used by somebody else (or on some OS)?
I don't understand what you mean, really. But I gather it should work just fine for you. Did you try it out to see how it performs?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Bit operations are often very fast on regular integers, which are also fast. I don't know what architecture you're using, but I'd go for the word solution.
Don't trust me, I can be wrong. So, profile, profile, profile. Make about 99999 iterations and try them out. Recently I have proved that alignment for data is necessary (aligned data gets fetched faster, since unaligned integers may have to be fetched from two words of memory and then shifted or masked out.)
> Which one will be faster?
Unless you're planning to do this millions of times a second, I would suggest you forget about micro-optimising until you've got something up and running.
You're basically wasting your time until you've got a complete program, with actual data to push through it. Anything you do before then is a crap-shoot.
Do what logically makes the most sense for your program, and let the compiler deal with it. Most modern compilers are pretty good at this sort of thing, so let them deal with the detail, why you find bigger things to concern yourself with.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Whether your standard error return type is an integer or a structure is a HUGE design decision and there's no excuse for NOT considering it completely, up front. I agree that performance of the solution is probably not the prime concern, but using a struct has some pretty big implications, biggest of which is that somebody may, in the future, add fields to the struct, or design new code under the assumption that the struct could be modified, if needed, and suddenly what used to be a minor concern about performance becomes a major one.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
I'll go for an integer solution. Easy, reliable and most likely faster.