The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How optimize "for" statement
Discuss How optimize "for" statement in the C Programming forum on Dev Shed. How optimize "for" statement C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 2nd, 2013, 09:08 AM
|
|
Registered User
|
|
Join Date: Jul 2011
Posts: 5
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
|

January 2nd, 2013, 09:22 AM
|
 |
Contributed User
|
|
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|