Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

21
Dec

Optimization by loop re-factoring

Related Blog Items

Instead of:

for(int i = 0; i < MAX; ++i)
if(i != ID)
{
// DO SOMETHING
}
}

Use:

for(int i = 0: i < ID; ++i)
{
// DO SOMETHING
}
for(int i = ID+1; i < MAX; i++>
{
// DO SOMETHING
}

Popularity: 9%

You need to log on to convert this article into PDF


Related Blog Items

6 Comments

  • Sridhar  said:

    for(int i = 0: i
    {
    // DO SOMETHING
    }

    In the above I think second for loop is meaningless. Please correct it.
    …Wizard


  • ponnada  said:

    Sridhar,
    Second loop is required, if you observe bit carefully, we are refactoring the loop to gain 1 cycle for each iteration, i.e. first loop process upto ID, second loop process from ID to MAX.

    Thanks!!


  • ponnada  said:

    However there is formatting mistake in second loop, that is i=”" should be i++.
    Thanks!!


  • Jok  said:

    I think you may want to write :

    Instead of:
    int i;

    for(i = 0; i


  • Jok  said:

    Sorry for the format, I think you may want to write :

    Instead of:
    int i;

    for(i = 0; i < MAX; i++)
    if(i != ID) {
    /* DO SOMETHING */
    }

    Use:
    int i;

    for(i = 0: i < ID; i++) {
    /* DO SOMETHING */
    }
    for(i = ID + 1; i < MAX; i++)
    {
    /* DO SOMETHING */
    }


  • Asthra  said:

    yes, thats right… I’ll correct it…


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image