Thread: C implementation doubt

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    C implementation doubt

    Hi,

    I am writing a parser code for the following format received:

    STX | CMD1|CMD2|DATA_LEN|DATA| ETX |

    Each 1 byte in len

    There are approximately 100 such unique Commands. And the data will vary based on the datalen, which also needs to be parsed.


    Coming to the problem:

    1. What is the adavantage of an if else over the switch case, besides the code readablity.
    I mean in terms of efficiency and memory consumption as memory is limited in my case a mere 92Kb.

    2. Is it optimal to use switch and case statements for each Command.

    ie, 100 commands = 100 cases.

    3. Is it optimal to use function pointers for the same.

    Thanks to all

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What is the adavantage of an if else over the switch case, besides the code readablity.
    Switch statements are (usually) implemented as a jump table, and are very efficient. A bunch of if statements will require several comparisons, so it will be slower.

    Is it optimal to use switch and case statements for each Command.
    Yes.

    Is it optimal to use function pointers for the same.
    I don't see what the need would be for function pointers in your situation.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whether a switch is implemented as a bunch of comparisons (similar to an if/else tree) or as a jump table depends on the number of close and number of far a part entries in the cases. If you have, for example,
    Code:
    switch(a)
    {
       case 1:
            ...
       case 2:
            ...
       case 4:
           .....
       case 5:
           ...
       case 6:
          ...
    }
    Then I'd expect a jump table.

    If the numbers are fairly far apart (and not trivially divided down to smaller numbers):
    Code:
    switch(a)
    {
       case 113:
            ...
       case 219:
            ...
       case 426:
           .....
       case 512:
           ...
       case 699:
          ...
    }
    then you can expect it to do a "binary chop" (start in the middle then go left/right depending on whether it is greater or lesser).

    The code generated by the compiler is almost always better (smaller) than what you can achieve on your own - at least beyond going for a complicated, hard-to-follow binary chop type code, which would be rather difficult to do for around 100 cases.

    Assuming your commands aren't very complicated for each case, I'd say 92KB should be doable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks all for your time...

    Quote Originally Posted by matsp View Post
    Then I'd expect a jump table.

    If the numbers are fairly far apart (and not trivially divided down to smaller numbers):
    Code:
    switch(a)
    {
       case 113:
            ...
       case 219:
            ...
       case 426:
           .....
       case 512:
           ...
       case 699:
          ...
    }
    then you can expect it to do a "binary chop" (start in the middle then go left/right depending on whether it is greater or lesser).

    The code generated by the compiler is almost always better (smaller) than what you can achieve on your own - at least beyond going for a complicated, hard-to-follow binary chop type code, which would be rather difficult to do for around 100 cases.

    Assuming your commands aren't very complicated for each case, I'd say 92KB should be doable.

    --
    Mats
    This means that the efficiency is lost if the numbers are far apart(Compared to an if else statement). As you can see that the command is 2 bytes , commands can go well upto 65k.

    In the above case will the case statements be go thru an round of sorting before moved to the jump table.I don't how jump table concept work can someone explain me or a good tutorial that explains the concept. I mean will their be a comparison of the switch number with each individual case number or does it uses an index to the case e.g. base+10 will move to the 10th case.


    Each command has its own data which has to be again parsed separately and put into a PDU and sent over RF.

    Coming to the function pointer

    Hence i am going to use separate command specific API.
    eg.
    Code:
    add()
    {
    };
    sub()
    {};
    
    uint8 char index;
    (function ptr) fptr[]=
    {
          &add,
          &sub
    };
    will this be an efficient approach.

    Thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a 16-bit command, that is sparsely filled, then you will (probably) not get a jump table. But also using a function pointer table will not be any better than using switch, since the table will be 256KB to take a 16-bit number as an index. My guess would be that your commands are in groups, in which case a switch with another switch inside it [probably make the second switch into a function] is most likely a good solution.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. class schema implementation doubt
    By newbie007 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2008, 04:42 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM