-
April 1st, 2004, 02:03 AM
#1
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!!!!!
-
April 1st, 2004, 02:23 AM
#2
Try and disassembly the code on the machine you are working on,
this might let know ...
-
April 1st, 2004, 02:48 AM
#3
I dont knw how 2 do it!!!Jst tell me which one is faster!!!
-
April 1st, 2004, 02:55 AM
#4
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 1st, 2004, 02:59 AM
#5
I need some xplanation on tht pleeez!!!!
-
April 1st, 2004, 03:14 AM
#6
Neither is faster, it only depends on what you put in it. Aside from that it's just a matter of syntax.
All the buzzt
CornedBee
-
April 1st, 2004, 03:58 AM
#7
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*.
-
April 1st, 2004, 04:21 AM
#8
Well...if you want to know what is faster...simply profile your code...
-
April 1st, 2004, 07:23 AM
#9
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:
Code:
mov ecx, 50
schl:
; Do Something here
loop shl
this will be if you are coding this loop:
Code:
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
-
April 2nd, 2004, 10:46 AM
#10
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.
-
April 2nd, 2004, 11:13 AM
#11
What u all say guys looks right. But if u try to measure something, it looks like some difference exists. Here some test code:
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?
-
April 2nd, 2004, 11:15 AM
#12
The while loop never runs, you forget to reset the index.
All the buzzt
CornedBee
-
April 2nd, 2004, 11:32 AM
#13
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......
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
April 2nd, 2004, 12:36 PM
#14
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.
-
April 2nd, 2004, 12:43 PM
#15
Something that does nothing is slower than nothing that does something.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|