Thread: evaluation of const variables

  1. #16
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    And the point of all this is to...?
    I'm making a program to read data from USB Devices (gamepads and joysticks).

    Code:
    while(!poll_interrupt)
    {
     switch(dev_type)
     {
     case 1:
     fetch data from dev1;
     break
     case 2:
     fetch data from dev2;
     break
     etc..
     }
     translate information
     send_it_somewhere
    }
    I would like to turn the code as efficient as possible to improve sampling times.

    The dev_type is an input parameter, but it doesn't change during runtime.
    Last edited by frs; 09-05-2010 at 01:35 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by frs
    I would like to turn the code as efficient as possible to improve sampling times.
    So you have this in mind?
    Code:
    switch (dev_type)
    {
    case 1:
        while (!poll_interrupt)
        {
            /* fetch data from dev1 */
            /* translate information */
            /* send it somewhere */
        }
        break;
    case 2:
        while (!poll_interrupt)
        {
            /* fetch data from dev2 */
            /* translate information */
            /* send it somewhere */
        }
        break;
    }
    But you are concerned about the code duplication?

    Personally, I would recommend that you test. Does any attempted optimisation really make a tangible difference to begin with, after compiler optimisations have been applied?

    Quote Originally Posted by frs
    The dev_type is an input parameter, but it doesn't change during runtime.
    As in it is actually a macro?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    So you have this in mind?
    Code:
    switch (dev_type)
    {
    case 1:
        while (!poll_interrupt)
        {
            /* fetch data from dev1 */
            /* translate information */
            /* send it somewhere */
        }
        break;
    case 2:
        while (!poll_interrupt)
        {
            /* fetch data from dev2 */
            /* translate information */
            /* send it somewhere */
        }
        break;
    }
    But you are concerned about the code duplication?
    Yes yes. That's it.

    Personally, I would recommend that you test. Does any attempted optimisation really make a tangible difference to begin with, after compiler optimisations have been applied?
    That is what I was going to do, I never did compiler optimizations..
    Probably I won't see any difference, but anyway is unnecessary to evaluate the switch statement in every poll cycle, and I can't see a solution without replicating the code over and over again.


    As in it is actually a macro?
    I know what a macro is but I didn't understand what you mean. sorry.
    Last edited by frs; 09-05-2010 at 04:40 PM.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why not a function?
    Code:
    void handle_poll_interrupt(int dev_type)
    {
        /* Do processing here */
    }
    ...
    while (!poll_interrupt)
    {
        handle_poll_interrupt(dev_type);
    }

  5. #20
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by rags_to_riches View Post
    Why not a function?
    Code:
    void handle_poll_interrupt(int dev_type)
    {
        /* Do processing here */
    }
    ...
    while (!poll_interrupt)
    {
        handle_poll_interrupt(dev_type);
    }
    Because the devices are treated much differently.

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Ah, obviously that wasn't clear in the code snippet. Sorry.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by frs
    I know what a macro is but I didn't understand what you mean.
    I am asking if dev_type is a preprocessor macro. More explicit, I am asking if it is input to the compiler (e.g., by using a -D or similiar option when invoking the compiler) rather than input to the program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM