Like Tree9Likes

How can i optimize this code

This is a discussion on How can i optimize this code within the C Programming forums, part of the General Programming Boards category; Hello, I just want to know how can i optimize this code. my requirement: i will receive frame of data ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66

    How can i optimize this code

    Hello,

    I just want to know how can i optimize this code.
    my requirement: i will receive frame of data via interrupt as: Length, Inverted Length, Data[Length], BCC.
    I have done the code and it is working.
    how can i optimize this?


    Code:
             if(Count == 1)
             {
                   length = ValueFromInterrupt;
                   Count++;
             }
             else if(Count == 2)
             {
                   InvertedLength = ValueFromInterrupt;
                   Count++;
             }
             else if(Count < (length+3))
             {
                   buffer[buffer_Count] = ValueFromInterrupt;
                   Checksum_BCC ^= ValueFromInterrupt;
                   Count++;
                   buffer_Count++;
             }
             else if(Count == (length+3))
             {
                   if(ValueFromInterrupt == Checksum_BCC)
                   {
                      Count=0;
                      Checksum_BCC = 0;
                   }
                   else
                   {
                      /*Delete the buffer data*/
                      buffer_Count=0;
                      Count=0;
                   }
              }

    Thank you in advance

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    What makes you think it isnt optimised anyway?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    66
    How can i change it into switch case?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by ArunS
    How can i change it into switch case?
    It does not look like a good candidate for that. I don't see anything obviously inefficient about the code snippet, though if you know which condition is most likely to be true, you could do a minor optimisation by testing for that condition first, but if this is not part of a performance bottle neck in your code, it really would not matter in the slightest.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by ArunS View Post
    I just want to know how can i optimize this code.
    By leaving it alone

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    As laser pointed out, unless you are actually seeing a performance bottle neck it isn't worth worrying about. The concept of code optimization, with the exception of some embedded system design and certain high performance applications, past writing clean manageable code is largely outdated and a throwback to the dark ages when compilers were clunky beasts and processors were stupid.

    In this day and age, most compilers are exceptionally good at what they do. In fact trying to out think the compiler often results is less optimal code because the compiler cannot figure out what you want to do and thus leaves the code as is; which in case you were wondering is not as efficient as it could be. Additionally, processors have evolved and are no longer the single cell amoebas they use to be; current branch prediction success is usually in the neighborhood of > 90%.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Minor optimization which will also make the code more readable. Readability's the only reason to do it since any non-broken optimizing compiler should figure it out for you any way.

    Code:
            {
                   Count=0;
                   if(ValueFromInterrupt == Checksum_BCC)
                   {
                         Checksum_BCC = 0;
                   }
                   else
                   {
                      /*Delete the buffer data*/
                      buffer_Count=0;
                   }
            }
    You might also try something more error-resistant than a simple XOR checksum, assuming you have access to both the sending and receiving code. CRC16 or 32 wouldn't be too much more work and way better.
    Last edited by KCfromNC; 08-05-2011 at 02:47 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( Count == length+3 )
    {
        Count = 0;
        if( ValueFromInterrupt == Checksum_BCC )
            Checksum_BCC = 0;
        else
            buffer_Count = 0;
    }
    else
    {
        if(Count == 1)
            length = ValueFromInterrupt;
        else if(Count == 2)
            InvertedLength = ValueFromInterrupt;
        else
        {
            buffer[buffer_Count] = ValueFromInterrupt;
            Checksum_BCC ^= ValueFromInterrupt;
            buffer_Count++;
        }
        Count++;
    
    }
    It's not any more efficient, but there are less repeated lines.


    Quzah.
    Last edited by quzah; 08-05-2011 at 11:10 PM. Reason: Thanks, grumpy, iMalc, +typo fix.
    iMalc likes this.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    Quote Originally Posted by quzah View Post
    It's not any more efficient, but there are less repeated lines.
    There is a minor concern with a missing curly brace, but that's just a typo.

    The only other change that might be relevant is changing post-increment to pre-increment in standalone statements. It doesn't make much difference with most modern compilers, and doesn't change the end result of the code, but is more semantically correct (a post-increment, semantically, involves a temporary that is not needed in this case).
    quzah and iMalc like this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It should also be noted that quzah's version increments Count before checking if it is 1 or 2, thus is incorrect in that manner also. I'm sure he'll fix that now.
    It also does stuff when Count > length+3, although we don't actually know if this case can actually occur.

    Case in point, if it aint broke - don't fix it!
    quzah likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    It should also be noted that quzah's version increments Count before checking if it is 1 or 2, thus is incorrect in that manner also. I'm sure he'll fix that now.
    No. I won't fix it, because it doesn't need fixing. All three of those cases increment Count, and don't do anything else with it. It's fine how I have it.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    Quote Originally Posted by quzah View Post
    No. I won't fix it, because it doesn't need fixing. All three of those cases increment Count, and don't do anything else with it. It's fine how I have it.
    Sorry; iMalc is correct.

    The code is incrementing Count, and then testing the value of Count. The values being tested against need to be incremented before testing, or do the incrementing of Count AFTER the other stuff.

    One of the "joys" of optimising code is that it is very easy to subtly change its meaning.
    Last edited by grumpy; 08-05-2011 at 11:07 PM.
    quzah likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    Sorry; iMalc is correct.
    I see. I also had it typoed in the first if check.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by grumpy View Post
    One of the "joys" of optimising code is that it is very easy to subtly change its meaning.
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
    -Brian Kernighan

    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by quzah View Post
    Code:
    if( Count == length+3 )
    {
        Count = 0;
        if( ValueFromInterrupt == Checksum_BCC )
            Checksum_BCC = 0;
        else
            buffer_Count = 0;
    }
    else
    {
        if(Count == 1)
            length = ValueFromInterrupt;
        else if(Count == 2)
            InvertedLength = ValueFromInterrupt;
        else
        {
            buffer[buffer_Count] = ValueFromInterrupt;
            Checksum_BCC ^= ValueFromInterrupt;
            buffer_Count++;
        }
        Count++;
    
    }
    It's not any more efficient, but there are less repeated lines.


    Quzah.
    This may be slightly slower if the common case is the last nested else since you're doing an extra if before you get there. Not that I'd bet it would make a measurable difference, though.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me optimize my code
    By lazyme in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2010, 04:05 AM
  2. Replies: 6
    Last Post: 12-19-2007, 12:24 PM
  3. Wanna Optimize my code?
    By Echo in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2005, 02:24 AM
  4. Sort strings in double linked list. (Optimize code)
    By netstar in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2005, 01:40 AM
  5. How to Optimize
    By cfrost in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2004, 09:07 AM

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