Thread: Simple Efficiency Query

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Post Simple Efficiency Query

    Hi

    Can anyone think of a batter way of doing this:

    Code:
    #define NONE       0x00
    #define PENDING    0x01
    #define REQUESTED  0x02
    
    typedef struct {
    	
    	uint8_t e2ConfigDispatch;
    	uint8_t deviceForceSend;
    	uint8_t commAckReq;
    	uint8_t commReq;
    	uint8_t sumMessageUpdate;
    	uint8_t sumMessageForward;
    	uint8_t LowPowere2ConfigDispatch;
    	uint8_t lowPowerForward;
    	uint8_t sendMessage;
    
    } SysServices_t;
    
    SysServices_t sysService;
    
    
    
    
    
    // This bit:
    
    bool sysServicePending() {
    
    	// If there are any services waiting return true
    	if (sysService.deviceForceSend == PENDING ||
    	    sysService.commAckReq == PENDING ||
    	    sysService.e2ConfigDispatch == PENDING ||
    	    sysService.commReq == PENDING || 
    	    sysService.sumMessageUpdate == PENDING ||
    	    sysService.LowPower2ConfigDispatch == PENDING ||
    	    sysService.sendMessage == PENDING ||
                sysService.sumMessageForward == PENDING) {
    		
                return true;
    
    	} // if
    
    	return false;
    
    } // sysServicePending
    One of the sysService struct variables may be set to PENDING, i don’t need to know which one, I just need to know if there is one pending. Other variables may be randomly set to REQUESTED...

    Any ideas would be greatly appreciated

    David

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If the result is true, how much work do you do as a result?

    I'm sure you could save a new nS on this code, but what would be the point if the follow-on work takes mS ?

    Leave the micro-optimisation to the compiler, and find more important things to improve by using a profiler.

    FWIW, you can improve things slightly by arranging the expression so that the most common things to be true are first.

    Other hacks will follow no doubt, once you've justified the necessity.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How about adding a "bool hasPending;"?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    Thanks both of you, just wanted to make sure I was not missing anything blindingly obvious, ill try re-arranging the if | sequence and look into adding a hasPending bit

    Thank you

    David

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Your struct has nine (9) flags but your ifs only check for 8. Maybe just a typo.

    If you instead palce all flags in a contiguous unsigned char array, you could use
    Code:
    memchr ( const void * ptr, int value, size_t num );
    to look for the PENDING code, if any.

  6. #6
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    You could have a single pending integer variable and allocate 1 bit for each service.
    Then check if pending is or is not 0.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> You could have a single pending integer variable and allocate 1 bit for each service.
    Then check if pending is or is not 0.

    Except that it looks like each variable is actually a tri-state (NONE/PENDING/REQUESTED). Even if it were though, using bitmasks is not always the best way to go. It's much less flexible and more awkward to work with. I'd say unless performance is really an issue here (and I suspect that is not) don't even bother, although having a single boolean flag "hasPending" as cyberfish suggested seems like a cleaner solution altogether.
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  2. Simple string query
    By ozzy34 in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2004, 11:40 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM
  5. O/l db and query interface
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-29-2001, 11:56 AM