Like Tree2Likes
  • 1 Post By iMalc
  • 1 Post By grumpy

how to optimize "for" statement

This is a discussion on how to optimize "for" statement within the C Programming forums, part of the General Programming Boards category; I have this code and I wont to optimize them: here is a scrolling menu with max 15 line and ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    how to optimize "for" statement

    I have this code and I wont to optimize them:

    here is a scrolling menu with max 15 line and loop must run for all station from database ( num_stations = number of stations from data base ).

    Code:
    #define MENU_SIZE 15
    
            int i;
            char text[ 32 ];
            const int num_stations = station_get_num_stations( cmd->station );
        osd_list_set_lines( cmd->osd, num_stations );
    
        osd_list_set_text( cmd->osd, 0, _("Station List") );
    
    
            for( i = 0; i < MENU_SIZE; i++ ) {
                    if( cmd->curmenupos < MENU_SIZE ) {
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s",  station_get_channel( cmd->station, i ), station_get_name(  cmd->station, i ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE ) { 
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s",  station_get_channel( cmd->station, i + 1 ), station_get_name(  cmd->station, i + 1 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE + 1 ) {  
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s",  station_get_channel( cmd->station, i + 2 ), station_get_name(  cmd->station, i + 2 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE + 2 ) { 
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s",  station_get_channel( cmd->station, i + 3 ), station_get_name(  cmd->station, i + 3 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
    
                    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    /* n = num_stations */
                    } else if ( cmd->curmenupos == MENU_SIZE + n ) {  
                       /* Station number + name */                    
                        snprintf( text, sizeof (text), "[%s] %s",  station_get_channel( cmd->station, i + n + 1 ), station_get_name(  cmd->station, i + n + 1 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    }
                    cmd->curmenusize = MENU_SIZE + 1
    my attempt:

    Code:
            #define MENU_SIZE 15
    
            int i, j;
            char text[ 32 ];
            const int num_stations = station_get_num_stations( cmd->station );
            osd_list_set_lines( cmd->osd, num_stations );
            osd_list_set_text( cmd->osd, 0, _("Station List") );
    
            for( i = 0; i < MENU_SIZE; i++ ) {
                for( j = 0; j < num_stations - MENU_SIZE; j++ ) {
                    if( cmd->curmenupos < MENU_SIZE ) {
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i ), station_get_name( cmd->station, i ) );
                       osd_list_set_text( cmd->osd, i + 1, text );
                    } else if( cmd->curmenupos == MENU_SIZE + j ) {
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + j + 1 ), station_get_name( cmd->station, i + j + 1 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    }
                }
            }
            cmd->curmenusize = MENU_SIZE + 1;
    Thanks
    Last edited by zradu; 01-02-2013 at 08:34 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    How about sharing with us what is the purpose of this code?

    //yes, I know that I can read and try to find out..., but...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by std10093 View Post
    How about sharing with us what is the purpose of this code?

    //yes, I know that I can read and try to find out..., but...
    I try to optimize from this:


    Code:
    #define MENU_SIZE 15
    
            int i;
            char text[ 32 ];
            const int num_stations = station_get_num_stations( cmd->station );
    
            for( i = 0; i < MENU_SIZE; i++ ) {
                    if( cmd->curmenupos < MENU_SIZE ) {
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i ), station_get_name( cmd->station, i ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE ) { 
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 1 ), station_get_name( cmd->station, i + 1 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE + 1 ) {  
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 2 ), station_get_name( cmd->station, i + 2 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    } else if ( cmd->curmenupos == MENU_SIZE + 2 ) { 
                        /* Station number + name */
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + 3 ), station_get_name( cmd->station, i + 3 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
    
                   ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
                    } else if ( cmd->curmenupos == MENU_SIZE + n ) {  
                       /* Station number + name */                    
                        snprintf( text, sizeof (text), "[%s] %s", station_get_channel( cmd->station, i + n + 1 ), station_get_name( cmd->station, i + n + 1 ) );
                        osd_list_set_text( cmd->osd, i + 1, text );
                    }
    
                   /* n = num_stations */
    Last edited by zradu; 01-02-2013 at 08:26 AM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    A loop executing 15 times is indeed a heavy loop needing optimisation.
    But assuming optimisaton for size, what about removing this loop entirely? You would save up to several hundred bytes in the code section.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by kmdv View Post
    A loop executing 15 times is indeed a heavy loop needing optimisation.
    But assuming optimisaton for size, what about removing this loop entirely? You would save up to several hundred bytes in the code section.
    Sorry, in my first post is rong code:

    #define NUM_STATIONS 15
    must be

    #define MENU_SIZE 15

    the loop must run for all number of stations from database: num_stations = stations in data base, menu_size = scrooling menu with max 15 line
    Last edited by zradu; 01-02-2013 at 08:22 AM.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    It doesn't change anything...

    Are you really sure what you want to optimise? Does this loop take ages to finish its job?

    Your loop seems to make no harm itself.
    I never put signature, but I decided to make an exception.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    perhaps a more elegant solution is what I'm looking for.

    This solution have a problem:

    Example:

    Number of stations in data base = 75

    1 If cursor in in first line of MENU and I wont to move up, rather than jumping to line 75, cursor jump to line 61. ( max stations - 15)
    2. If I scroll down all is fine: when cursor get line 75 and move down one line, jump to line 1 in MENU what is right.
    Last edited by zradu; 01-02-2013 at 08:52 AM.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    By saying "optimise" you make other think about code optimisation (e.g., for speed), which is something completely different.
    It is impossible to guess what you want to do by looking at a piece of broken and incomplete code. You might want to use an array to store some information, and instead of if-else statements, access that array at a specified index (index = cmd->curmenupos).
    I never put signature, but I decided to make an exception.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    "Optimise for code size" is the idea here I believe.

    What is wrong with the attempt you posted already?
    stahta01 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"

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,582
    Obvious (to me at least) question is the value of "cmd->curmenupos" changing inside the loop?

    If not changing, I see no need for the "else if" statements being used. My hint only applies if value is not changing inside loop.

    Note: I am guessing this is a homework assignment; therefore I am not giving you anymore help on it.

    Hint: The answer is more a math answer than a programming logic answer.

    Tim S.
    Last edited by stahta01; 01-02-2013 at 08:58 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    Obvious (to me at least) question is the value of "cmd->curmenupos" changing inside the loop?

    If not changing, I see no need for the "else if" statements being used. My hint only applies if value is not changing inside loop.

    Note: I am guessing this is a homework assignment; therefore I am not giving you anymore help on it.

    Hint: The answer is more a math answer than a programming logic answer.

    Tim S.
    Hello,
    Yes, "cmd->curmenupos" changing inside the loop

    MOVE UP:
    Code:
    cmd->curmenupos = (cmd->curmenupos + cmd->curmenusize - 1) % (cmd->curmenusize);
    MOVE DOWN:
    Code:
    cmd->curmenupos = (cmd->curmenupos + 1) % (cmd->curmenusize);



    This command ( cmd->curmenupos ) move up or down with one line in osd menu.

    My problem is:
    If cursor is in first line of OSD MENU and I want to move up, rather than jumping to line 75, cursor jump to line 61.

    See screenshot:
    Name:  screenshot.jpg
Views: 104
Size:  38.5 KB

    If I scroll down all is fine: when cursor get line 75 and move down one line, jump to line 1 in MENU what is right.

    Thanks
    Last edited by zradu; 01-03-2013 at 09:17 AM.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by zradu View Post
    Hello,
    My problem is:
    If cursor is in first line of OSD MENU and I want to move up, rather than jumping to line 75, cursor jump to line 61.
    Thanks
    I give up - this does not make any sense. Moving from first to last would require moving up one, and then moving 14 down...

    You have to explain your goals in details. Read your first post once again, it asks for something completely different...
    I never put signature, but I decided to make an exception.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,310
    If you want help with optimising a loop, it would be helpful if you provide all information relevant to optimising that loop.

    On a visual examination of your code, it appeared that cmd->curmenupos does not change, hence stahta01's suggestion. You have now revealed that it does change.

    As such, you are wasting people's time. I suspect deliberately.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimize?
    By nuko in forum C Programming
    Replies: 3
    Last Post: 12-21-2011, 12:14 AM
  2. Please help me optimize my code
    By lazyme in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2010, 03:05 AM
  3. Can I further Optimize this script?
    By gross100 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 01:40 AM
  4. Help me optimize
    By sand_man in forum Game Programming
    Replies: 14
    Last Post: 05-10-2005, 05:19 PM
  5. How to Optimize
    By cfrost in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2004, 08:07 AM

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