21
Dec
Optimization by loop re-factoring
Related Blog Items
- Detecting a loop in single linked list
- C++ Basics Tutorial - Lesson 3
- Sorting a Linked List
- Clipping and Clamping with out 'if condition'
- C++ Advanced Tutorial - Lesson 7
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 - Detecting a loop in single linked list
- C++ Basics Tutorial - Lesson 3
- Sorting a Linked List
- Clipping and Clamping with out 'if condition'
- C++ Advanced Tutorial - Lesson 7
Related Blog Items
- Detecting a loop in single linked list
- C++ Basics Tutorial - Lesson 3
- Sorting a Linked List
- Clipping and Clamping with out 'if condition'
- C++ Advanced Tutorial - Lesson 7
for(int i = 0: i
{
// DO SOMETHING
}
In the above I think second for loop is meaningless. Please correct it.
…Wizard
December 28th, 2006 at 3:29 pm
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!!
December 30th, 2006 at 8:37 pm
However there is formatting mistake in second loop, that is i=”" should be i++.
Thanks!!
December 30th, 2006 at 8:38 pm
I think you may want to write :
Instead of:
int i;
for(i = 0; i
April 3rd, 2007 at 3:25 pm
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 */
}
April 3rd, 2007 at 3:27 pm
yes, thats right… I’ll correct it…
April 3rd, 2007 at 4:54 pm