CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Apr 2004
    Posts
    123

    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!!!!!

  2. #2
    Join Date
    Feb 2004
    Posts
    45
    Try and disassembly the code on the machine you are working on,
    this might let know ...

  3. #3
    Join Date
    Apr 2004
    Posts
    123
    I dont knw how 2 do it!!!Jst tell me which one is faster!!!

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    both!!!!
    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."

  5. #5
    Join Date
    Apr 2004
    Posts
    123
    I need some xplanation on tht pleeez!!!!

  6. #6
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    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

  7. #7
    Join Date
    Aug 2002
    Location
    United States
    Posts
    729
    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*.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...if you want to know what is faster...simply profile your code...

  9. #9
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    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
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  10. #10
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    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.

  11. #11
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    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?
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  12. #12
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    The while loop never runs, you forget to reset the index.
    All the buzzt
    CornedBee

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  14. #14
    Join Date
    Apr 2004
    Posts
    11
    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.

  15. #15
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Something that does nothing is slower than nothing that does something.
    All the buzzt
    CornedBee

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured