473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to optimize for loop?

I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?

Nov 15 '05 #1
10 14323
>I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


Assuming for the moment that do-something does not contain break or
continue, and it uses but does not alter i, some people consider this an
optimization (loop unrolling):

do {
i = 0;
do-something;
i = 1;
do-something;
i = 2;
do-something;
i = 3;
do-something;
i = 4;
do-something;
i = 5;
do-something;
i = 6;
do-something;
i = 7;
do-something;
i = 8;
do-something;
i = 9;
do-something;
i = 10;
do-something;
i = 11;
do-something;
i = 12;
do-something;
i = 13;
do-something;
i = 14;
do-something;
i = 15;
do-something;
i = 16;
do-something;
i = 17;
do-something;
i = 18;
do-something;
i = 19;
do-something;
i = 20;
} while (0);

Gordon L. Burditt
Nov 15 '05 #2
Gordon Burditt wrote:
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?

Assuming for the moment that do-something does not contain break or
continue, and it uses but does not alter i, some people consider this an
optimization (loop unrolling):

do {
i = 0;
do-something;
i = 1;
do-something;
i = 2;
do-something;
i = 3;
do-something;
i = 4;
do-something;
i = 5;
do-something;
i = 6;
do-something;
i = 7;
do-something;
i = 8;
do-something;
i = 9;
do-something;
i = 10;
do-something;
i = 11;
do-something;
i = 12;
do-something;
i = 13;
do-something;
i = 14;
do-something;
i = 15;
do-something;
i = 16;
do-something;
i = 17;
do-something;
i = 18;
do-something;
i = 19;
do-something;
i = 20;
} while (0);

Gordon L. Burditt

In the same way, googlr for Duff's device
Zara
Nov 15 '05 #3
Hi,

It depends on what is "do-something". The optimization can be loop
unrolling, moving loop envarient code out side of the loop, common
sub-expression elimination,.... It depends on your code inside the
loop.

Regards,
Raju

Bhan wrote:
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


Nov 15 '05 #4
"Bhan" <sw*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


The above is exactly equivalent to:

i = 0;
while (i < 20) {
do-something;
++i;
}

It is obvious that the first time the condition is tested (before entering
the loop) it will be true, so there is no need to actually do the test. The
test can be moved to the bottom of the loop:

i = 0;
do {
do-something;
++i;
} while (i < 20);

If the value of i is not used except to control the number of iterations,
the following may be better in some cases:

i = 20;
do {
do-something;
} while (--i);

Another possibility already mentioned is loop unrolling. This increases code
size but may not actually increase performance. In particular, unrolling
tests (eg if "do-something" contains one or more "if" statements) may
actually decrease performance.

The loop may be partially unrolled, eg:

/* if the value of i is used: */
i = 0;
do {
do-something;
++i;
do-something;
++i;
} while (i < 20);

/* or, if the value of i is not used */
i = 0;
do {
do-something;
do-something;
++i;
} while (i < 10);

Or the loop may be fully unrolled, in which case there is no loop anymore.

However, all of this is best left to the compiler to figure out, until and
unless such optimisation helps a previously-identified performance
bottleneck.

Alex
Nov 15 '05 #5
On Mon, 05 Sep 2005 21:37:36 -0700, Bhan wrote:
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


If the loop can be usefully optimised the compile will propbably do so.
There's no reason to assume that an EQUIVALENT while or do-while loop will
result in different generated code.

Lawrence

Nov 15 '05 #6
Alex Fraser wrote:
"Bhan" <sw*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?

The above is exactly equivalent to:

i = 0;
while (i < 20) {
do-something;
++i;
}


.... provided `do-something' doesn't `continue' ...

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #7
"Eric Sosman" <es*****@acm-dot-org.invalid> wrote in message
news:td********************@comcast.com...
Alex Fraser wrote:
"Bhan" <sw*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


The above is exactly equivalent to:

i = 0;
while (i < 20) {
do-something;
++i;
}


... provided `do-something' doesn't `continue' ...


Indeed, I should have thought of that. All of what I wrote assumes no
"continue".

Alex
Nov 15 '05 #8
Bhan wrote on 06/09/05 :
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


Not really. You could eventually unroll the loop, but the benefit
should be negligible. Do measurements to be sure.

do-something; /* #1 */
do-something; /* #2 */
do-something; /* #3 */
....
do-something; /* #19 */
do-something; /* #20 */

(if you have a unixoïd box, use the times command... very handy)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #9

"Bhan" <sw*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I heard for(i=0;i<20;i++)
{
do-something;
}

Can be optimized.
Is this can really optimized by an equivalent for loop or with while or
do while loops?


Only if you're using a primitive compiler. Modern compilers will generate
the same code for:

for (i=0;i<20;i++) { ... }

i=0; while(i<20) { ... i++; }

i=0; do { ... } while (++i < 20);

and even:

i=0;
L1: if (i >= 20) goto L2;
{ ... }
i++;
goto L1;
L2:

Try the various forms and output the assembler language, and compare. If
you're interested in optimizing code, you'll need a precise way of timing it
to see how you're doing:
http://www.digitalmars.com/techtips/timing_code.html

-Walter
www.digitalmars.com free C, C++, D compilers
Nov 15 '05 #10
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
Not really. You could eventually unroll the loop, but the benefit
should be negligible. Do measurements to be sure.


And the compiler probably already knows how much to unroll it.

-- Richard
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
10
by: MariusI | last post by:
I stumbled over an optimization (or lack of one, to be specific) when viewing IL opcodes generated by the compiler using ms .net 2003. I was testing fast pixel manipulation using Bitmap.LockBits...
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, Out of curiousity, I sometimes look at the produced assembly after compilation in release mode. What you often see, is that CPP, always fully addresses registers to copy values from a to...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
26
by: a.mil | last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff and I encountered the following problem: When I have a for loop like this: b=b0; for (a=0,i=0;i<100;i++,b--) { if (b%i)...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
11
by: inihility | last post by:
This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster. for (int i = 0; i < 50; i++) { a = getNumber(i); } Right now it...
14
by: andreyvul | last post by:
I have this loop (all variables are pointers): for (foo = bar; foo baz; foo--) *(foo+1) = *foo; How do I optimize the pointer swap so that it uses -- and ++ or unary +- instead of +1 (if possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.