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.
Can't be proven, you have to get nothing to do something.Quote:
Originally posted by CornedBee
Something that does nothing is slower than nothing that does something.
You can't do that, or if you could you would be famous.
As the laws of physics states: Zero Equals Zero
Zero Point Energy, anyone?
Of course it depends how strictly you see "nothing". :)
| something
Theres something on the left, there aint anything on the right!
Okay, this is as with air IRL, theres actually SPACES there, but you know what i mean ;)
If nothing, zip, zero, nada, nothing RIGHT now creates a big bowl of candy besides me on my table, i'll eat my words. :D
No, you'll eat the candy ;)
dont tell nothing that! (or is it noone?)
:D
[voice=singsang]Shan't say nothing if you don't say please![/voice]
I think you are confusing the measurer with the measured.
case 1. (something has mass)
electron just hanging out and a photon in THE VOID.
the electron has mass and is therefore something.
The photon has no mass and is therefore nothing.
The photon moves at a speed which bounds all possible speeds
of the electron.
ergo
Something that does nothing can be slower than nothing that does something.
case 2. (if "doing" means "can be calculated", something means energy)
photon in THE VOID
those great big scissors (blades the size of the galaxy).
close the scissors.
Then the scissors are imaginary and have no energy, ergo nothing.
The photon has energy, therefore something
Well the point of contact of the blades of the scissor, moves faster
than the photon could every move.
Nothing that does something can be faster than something that does something
nothing can't do anything:
Ex nihilo nihil fit.:)
call this bold statement P1Quote:
nothing can't do anything
can we further agree that.
P2. if A does X, then X is something.
Now given P1 and P2 .
1. ~anything = nothing
2. P1 and statement 1 imply nothing does nothing.
3. P2 and statement 2 imply that nothing = something
reductio ad absurdum.
Just to be serious for a moment.
There are many examples in language where
a given formula is well formed, but has no symantic value.
The given sentence is meaningless.
e.g.
Nothing does x
Y exists. (all statements of ontology)
Y knows X (all statements of epistimology)
and the ever present combination of the above coupled with a symantic judgement.
Y knows that it is TRUE that X exists.
MEANINGLESS
:blush:Quote:
Originally posted by CornedBee
The while loop never runs, you forget to reset the index.
i probably forgot it. have u tried to test the problem WITH the initilization of i = 0; ? looks like there are differences for large numbers of iterations...