Cud u pleez tell me which loop is faster!!!!
A While Loop Or A For Loop????
Kindly mention the details of y it's so!!!!!
Printable View
Cud u pleez tell me which loop is faster!!!!
A While Loop Or A For Loop????
Kindly mention the details of y it's so!!!!!
Try and disassembly the code on the machine you are working on,
this might let know ...
I dont knw how 2 do it!!!Jst tell me which one is faster!!!
both!!!!
I need some xplanation on tht pleeez!!!!
Neither is faster, it only depends on what you put in it. Aside from that it's just a matter of syntax.
well your grammar and the way you ask your question is an immediate roadblock to getting a reply......
they've already told you that neither is really faster. a loop is a loop is a loop is a loop. the difference is what is being looped.
for(;i<b;){}
while(i<b){}
same loop. it's just a syntax difference where the for loop has other optional parameters to allow you to do stuff every cycle *generally used for incrementing*.
Well...if you want to know what is faster...simply profile your code... :cool:
a while loop will be translated into an assembler code with an jump (jz, jg, jl etc.) sometimes the compiler turns the for loop into a "loop" with cx:
this will be if you are coding this loop:Code:mov ecx, 50
schl:
; Do Something here
loop shl
only for loops with a constant value at the comparission ...Code:for ( int i = 0; i <= 50; i++)
{
// Do something here
}
so it depends wich loop you have
exactly....
What is a loop.. A loop either a for or a while is just decrementing the code pointer to a new address. What makes a for, or while more or less efficient is
#1 the code being executed..
#2 the index being updated...
now if your using an int for the index and you create it inside of your for and not inside of your while that will make the for slower on the initial run, but that doesn't mean it's less effiecient. IT's just doing more....
Like everybody else said, for and while can use the exact same assembler or machine code depending upon how you write them. Nothing faster or slower about either. To get a better answer you'll need to give specific examples to eveluate.
What u all say guys looks right. But if u try to measure something, it looks like some difference exists. Here some test code:
this gives me (in release mode), that for loop is much slower.Code:#include <stdio.h>
#include <time.h>
#define MAX 10000
void do_something();
int main()
{
int i;
int s;
int T1,T2;
int cur_step;
cur_step = MAX;
while (cur_step<MAX*10)
{
// for loop:
T1 = clock();
for (i=0;i<cur_step;i++)
{do_something();}
T2 = clock();
printf("%d\t",T2-T1);
// while loop:
T1 = clock();
while (i<cur_step)
{i++;do_something();}
T2 = clock();
printf("%d\n",T2-T1);
cur_step+=MAX;
}
return 0;
}
void do_something()
{
int i;
int s = 0;
for (i=0;i<10000;i++)
s+=i;
}
Any ideas?
The while loop never runs, you forget to reset the index.
So something that does nothing takes less time than something that does something.......
Now I wonder....Is nothing that does something faster or slower than something that does nothing or nothing that does nothing......
Depends how you define time.Quote:
Originally posted by TheCPUWizard
So something that does nothing takes less time than something that does something.......
Now I wonder....Is nothing that does something faster or slower than something that does nothing or nothing that does nothing......
If nothing does nothing, and you define the speed it does it with as how long til its finished doing nothing, it is infinitly SLOW by defination.
But if you define its speed by how long from start to end, you will never get the result, since noone know when nothing started doing nothing and when its going to be finished.
Something that does nothing is slower than nothing that does something.