Return 16bit structure or 16bit word?

This is a discussion on Return 16bit structure or 16bit word? within the C Programming forums, part of the General Programming Boards category; Which one is faster (and why). Returning structure: Code: struct { unsigned char foo1; unsigned char foo2; } or returning ...

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    Return 16bit structure or 16bit word?

    Which one is faster (and why). Returning structure:
    Code:
    struct
    {
    unsigned char foo1;
    unsigned char foo2;
    }
    or returning unsigned int. Lets assume that they both take 16bits. Which one will be faster?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  3. #3
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    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)?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    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.)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by Salem View Post
    > 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.
    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);
    //}

  8. #8
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    I'll go for an integer solution. Easy, reliable and most likely faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 12:29 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21