#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    How 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 ).


    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 ) {
                        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 ) {
                        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 ) {
                        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 ) {
                        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 ) {
                        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 */
    my attempt:

    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 */
    can someone help me with a better solution?

    Thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Wait for the other forum to finish chewing on it first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo