473,394 Members | 1,841 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,394 software developers and data experts.

For loop and while loop. which one is faster?

For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.

Mar 26 '07 #1
6 10786
mg****@gmail.com wrote:
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.
Unless you're designing a compiler, don't worry about that. Worry about the
algorithms instead.

That said, the only possible difference between a for loop and a while loop
is the jump prediction in your architecture's code instruction set. e.g.
the processor can suppose that the condition for a while loop is going to
be true, so it feeds the instructions after the conditional jump (that is,
it's supposing that the jump condition will be successful) and starts
running them in the first stages of the instruction pipeline, saving a
couple of pipeline stages.

Do you have to worry about processor pipelines and jump prediction? Not at
all, unless you're into assembler, or heavy low-level programming. If you
haven't understood a word of the last paragraph, don't worry about loop
speed.
In other words: trust your compiler (or JIT compiler, or interpreter). It
knows about optimizations of conditional jumps better than you (the same
applies to memory caching). Focus on the algorithmics if you want to
shorten processor time.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Give a man a fire and he'll be warm for a day; set him on fire and he'll be
warm the rest of his life.
Mar 26 '07 #2
In article <eu**********@hercules.cohp1>,
Iván Sánchez Ortega
<ivansanchez-alg@rroba-escomposlinux.-.punto.-.orgwrote:
mg****@gmail.com wrote:
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.

Unless you're designing a compiler, don't worry about that. Worry about the
algorithms instead.

That said, the only possible difference between a for loop and a while loop
is the jump prediction in your architecture's code instruction set. e.g.
the processor can suppose that the condition for a while loop is going to
be true, so it feeds the instructions after the conditional jump (that is,
it's supposing that the jump condition will be successful) and starts
running them in the first stages of the instruction pipeline, saving a
couple of pipeline stages.

Do you have to worry about processor pipelines and jump prediction? Not at
all, unless you're into assembler, or heavy low-level programming. If you
haven't understood a word of the last paragraph, don't worry about loop
speed.

In other words: trust your compiler (or JIT compiler, or interpreter). It
knows about optimizations of conditional jumps better than you (the same
applies to memory caching). Focus on the algorithmics if you want to
shorten processor time.
This is quite right. I never worry about such things - that's the
computer's job.

I use whichever is appropriate - try, in other words, to give a hint to
yourself or whoever is going to look at that code in a year's time. If I
have n items and I want to do something for each of them, I use a for
loop. If I need to loop around until some condition is satisfied (maybe
one of several conditions in different parts of the loop), then I use a
while.

-- tim
Mar 27 '07 #3
mg****@gmail.com wrote:
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.
Why don't you test them if its that important?

C.
Mar 28 '07 #4
Also sprach mg****@gmail.com:
For loop and while loop. which one is faster?
I don't think it makes much of a difference internally.

for ( $i = 0; $i < 10; $i++ )
{
// do stuff
}

is probably treated just like the - less elegant -

$i = 0;
while ( $i < 10 )
{
// do stuff
$i++;
}

with no performance gain or loss. On the other hand, it makes a huge
difference in performance if you write

for ( $i = 0; $i < count( $a ); $i++ ) { /* do stuff */ }

or

for ( $i = count( $a ); $i--; ) { /* do stuff */ }

the latter being much faster (function calls - count() in this example -
cost time and the second version is one statement shorter). Of course, it
iterates backwards over $a, but there are many cases where this does not
matter. So, if you want to optimize your code, reduce the number of function
calls, reduce the number of statements and reduce the number of variables
(especially for intermediate results).

Greetings,
Thomas
Mar 29 '07 #5
mg****@gmail.com wrote:
: For loop and while loop. which one is faster?
: I see many articles fighting over it and different people come up with
: different results.

What ever you find, it will likely be wrong next year, And different again
the year after that.

Use the construct that makes the most sense.

Mar 30 '07 #6
On Mar 27, 1:45 am, mgc...@gmail.com wrote:
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.
a bit late, but...
AFAIK it is faster to count down than up. Dont know about while or
for, but they are pretty much the same.

Personally I use:
$i=88883467568457642645;
while(0<$i--)
blabla;
Mar 30 '07 #7

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

Similar topics

4
by: bourgon | last post by:
Working on some new code, I'm coming across WHILE loops used instead of cursors. I was curious if anyone had any stats on how the speed of doing this compares to the speed of a cursor. I...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
34
by: sushant | last post by:
hi all, suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++) { for(j=0;j<=10;j++) { some code; }
6
by: Brian | last post by:
It's a minor thing, but I'm confused on the increment operator in a for loop. I think most usually write: for (int i = 0; i < something; i++) { .... } But, in my book (from MSPress) it...
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...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
8
by: malkarouri | last post by:
Hi everyone, I have an algorithm in which I need to use a loop over a queue on which I push values within the loop, sort of: while not(q.empty()): x = q.get() #process x to get zero or more...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.