Which Is Faster!
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!!!!!
[142 byte] By [
mmx_nexus] at [2007-11-18 18:34:58]

# 1 Re: Which Is Faster!
Try and disassembly the code on the machine you are working on,
this might let know ...
# 2 Re: Which Is Faster!
I dont knw how 2 do it!!!Jst tell me which one is faster!!!
# 3 Re: Which Is Faster!
both!!!!
# 4 Re: Which Is Faster!
I need some xplanation on tht pleeez!!!!
# 5 Re: Which Is Faster!
Neither is faster, it only depends on what you put in it. Aside from that it's just a matter of syntax.
# 6 Re: Which Is Faster!
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*.
# 7 Re: Which Is Faster!
Well...if you want to know what is faster...simply profile your code... :cool:
# 8 Re: Which Is Faster!
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:
mov ecx, 50
schl:
; Do Something here
loop shl
this will be if you are coding this loop:
for ( int i = 0; i <= 50; i++)
{
// Do something here
}
only for loops with a constant value at the comparission ...
so it depends wich loop you have
# 9 Re: Which Is Faster!
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.
JMS at 2007-11-9 0:39:47 >

# 10 Re: Which Is Faster!
What u all say guys looks right. But if u try to measure something, it looks like some difference exists. Here some test 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;
}
this gives me (in release mode), that for loop is much slower.
Any ideas?
# 11 Re: Which Is Faster!
The while loop never runs, you forget to reset the index.
# 12 Re: Which Is Faster!
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.....
# 13 Re: Which Is Faster!
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.....
Depends how you define time.
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.
# 14 Re: Which Is Faster!
Something that does nothing is slower than nothing that does something.
# 15 Re: Which Is Faster!
Originally posted by CornedBee
Something that does nothing is slower than nothing that does something.
Can't be proven, you have to get nothing to do 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?
# 16 Re: Which Is Faster!
Of course it depends how strictly you see "nothing". :)
# 17 Re: Which Is Faster!
| 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
# 18 Re: Which Is Faster!
No, you'll eat the candy ;)
# 19 Re: Which Is Faster!
dont tell nothing that! (or is it noone?)
:D
# 20 Re: Which Is Faster!
Shan't say nothing if you don't say please!
# 21 Re: Which Is Faster!
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
# 22 Re: Which Is Faster!
nothing can't do anything:
Ex nihilo nihil fit.:)
AvDav at 2007-11-9 0:53:07 >

# 23 Re: Which Is Faster!
nothing can't do anything
call this bold statement P1
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.
# 24 Re: Which Is Faster!
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
# 25 Re: Which Is Faster!
Originally posted by CornedBee
The while loop never runs, you forget to reset the index.
:blush:
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...