C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 2nd, 2013, 09:08 AM
zradu zradu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2011
Posts: 5 zradu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 27 sec
Reputation 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

Reply With Quote
  #2  
Old January 2nd, 2013, 09:22 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 49 m 48 sec
Reputation Power: 1774
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How optimize "for" statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap