which is the fastest loop?
By gagana
@gagana (757)
India
11 responses
@dholey (1383)
• India
15 Jan 07
i know the truth, all the loop executes in similar way (their execution time is same), we have to make initialization of variable, we have to check a condition and we have to increase or decrease value, the only difference is these points are handled in different lines in different loops, noting else, if you want to make fast loop you have to make a register variables, it takes less time as it is in the memory of cpu.
but, while do wile and for loop has same execution time, the advantage i for loop is only convenience of putting all three main points of loop in one line nothing else.
@dholey (1383)
• India
20 Jan 07
THERE IS NO DIFFERENCE ALL THREE POINTS (INITIALIZATION, CONDITION AND INCREMENTATION) are handled in different time so even in for loop it is same the control initializes the variable then checks the condition then runs the body then increments or decrements the value that is same as while , the only advantage is.... in for we can see all thee points in one single line where in other loops they are in different line ... so there is no difference in speed of different loops
@pvinay (210)
• India
20 Jan 07
while statment is the fastest
because let
for (i=0;i=10;i++){ statments....}
there r three two statments which r excuted at the start first is i=0; initialization and second is i=10;comparision now if comparision statment is true then inside statments are excuted . so at least two statment will excuted in for loop .now in
do while loop
do{ statments....}while(i=10)
so all body statment r excuted atleast one time weather comparision statment is true or false. so more stament r excuted as compare to for loop .
now in while loop
while(i=10){ statments....}
in while loop if coparison statment is false (i=10)then body statment is not excuted so atleast one statment is excuted in this case .
so u see that if first time comprasion statment is false
for loop excuted ......2 statments (initialization and comparision)
do while excuted......whole body statment
while loop excuted....1 statment(comparision)
so while statment is the fastest one
@tmaheshwari (170)
• India
17 Jan 07
i think all the loops are same in fastness.
but a compiler can does a little bit optimization in case of For loop because it know some condition in advance whereas in while to do-while loop comppiler is not aware of the increment values.
@girish3110 (138)
• Mauritius
20 Jan 07
It is the For loop because it knows how many times it should carry on.
@winter_man (407)
• Malaysia
15 Jan 07
Oh this is the programming or computer language issue..I hate my programming subject so much. I took C++ Programming and I got D- for that subject! I hate it so much!
@coolvin (21)
• India
15 Jan 07
I dont understand what u mean by fastest loop.
Does mean the block of code in the loop ,or the efficiency of the for ,while statements.
anyway,
Do-while if the loop executes only once , because a comparison comes only at the end of the loop once in do-while when translated to machine code.
for means (usually) initialization also included with the loop.so slower.
But,speed of execution depends on a specific implementation.it varies with number of loops or how/when you initialize and increment counters ...
@raveemenon (1071)
• India
15 Jan 07
without doubt it is 'For'. then it depends upoon the circumstance i hope.let linguists comment!
@stonehr (818)
• Croatia (Hrvatska)
15 Jan 07
Very interesting question you have. I can't say that for loop will be faster than while.Anyhow You can't always use for loop. In many cases you must use while loop.If You use while than You'll have following: x++; while x=100{... exit;}, so same elements You have in for loop also. for (x=0; x=100; x++; ){....}
You always have a counter and some condition that is checked everytime after is variable changed. So it would be great to avoid use of loop whenever you can.