1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Optimising C snippet

Discussion in 'C' started by suin, Mar 4, 2010.

  1. suin

    suin New Member

    Joined:
    Mar 4, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Dear Friends!
    Can Anybody help me to optimise the following c code size.
    I have list of macro definition which i have to pass as argument to a function

    example:

    #define ABC_1 123
    #define ABC_2 124
    #define ABC_3 125
    #define ABC_4 126
    #define ABC_5 127


    int main(void)
    {
    Function(ABC_1);
    Function(ABC_2);
    Function(ABC_3);
    Function(ABC_4);
    Function(ABC_5);
    }


    i tried token cancatenation but it doesn't help me while using loop.


    Thanks in advance
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    The above code cannot be optimized becoz the # define is taken care by preprocessor.. Preprocessor knows nothing other that replacing the variable with the actual value before the compiler take the responsibility of compiling the code...nd since the logic of concatenation is never clear until and unless the code comes under the compiler, the preprocessor fails to replace the manipulated part with the actual value...
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    ABC_1~5 are consecutive so you could use a for loop:
    Code:
    for (int i=ABC_1; i<=ABC_5; i++)
      Function(i);
    
    Not sure it would be much smaller though, and it would definitely be slightly slower. Why do you think you need to "optimise" five function calls for size? There is NO WAY profiling will have shown this to be a bottleneck.
     
  4. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    what type of programming is this??:lipsrseal
     
  5. suin

    suin New Member

    Joined:
    Mar 4, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thanks for your reply. it is not a consecutive number.
     
  6. Abinila

    Abinila New Member

    Joined:
    Feb 20, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Hi suin, You mentioned that "it doesn't help me while using loop".Can you give me what you tried.If the numbers are not consecutive looping is very difficult in your case. So tell me what you have tried.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You'll need to state the goal of the optimisation. The code as you've given it is pretty efficient. But the implication is that it doesn't do something you want it to. What is that?
     

Share This Page