CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Lightbulb which loop is faster?

    Hi, i had this idea that one loop might be faster than another, in terms of binary operations or w/e, but im not sure so i will ask. Suppose you have high performance code and you want the best loop. The loops are doing the same thing but my thought was that there underlying structures that operate the loops, one must be more efficient. I dont have the tools to test the speed and memory/bit usage of these, i was looking for more of a theoretical explanation of which would be faster. Two Examples.

    Code:
    //While loop
    
    int i=0;
    while(i<1000000)
    {
          i++;
    }
    
    
    //For loop
    
    for(int i=0;i<1000000;i++){/**/}
    
    
    
    //I did not compile this

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: which loop is faster?

    They only differ in the scope of i.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: which loop is faster?

    Quote Originally Posted by g.eckert View Post
    Hi, i had this idea that one loop might be faster than another, in terms of binary operations or w/e, but im not sure so i will ask. Suppose you have high performance code and you want the best loop. The loops are doing the same thing but my thought was that there underlying structures that operate the loops, one must be more efficient.
    Why do you say "must"? It all depends on what the compiler does with that code. The compiler could optimize one over the other or make the resulting code exactly the same.

    It is pointless to ask "which loop is faster", "is using "else" faster than "switch", and questions like that when you're given a high-level language such as C++. What makes more sense to ask is "is this algorithm faster?" "is this sort faster than this sort?" "is this search technique faster than that search technique", etc.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: which loop is faster?

    I understand the question is somewhat pointless, i guess what i was asking is, when the compiler translates the C++ loops down into the machine language, is the resulting machine language the same for each loop or do they differ? Which you answered this.

    I was also wondering where i could learn more about that. My programming classes rarely go into these topics, what is actually happening underneath the code. What would you suggest (reading) or classes that explain memory storage and ALU operations and things like that. My community college is very limited in CPS courses.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: which loop is faster?

    Once upon a time, when I was coding for 68000 microprocessors, I found that counting down to zero was slightly faster than counting up as the loop contained less instructions / clock cycles due to the decrement instruction also setting the flag bits.

    Nowadays I would just try the different code and time it. That's assuming I'd identified that bit of the code was a speed bottleneck.

    With modern compilers all you can assume is that they will produce a list of instructions that will produce the same effect as your source code. Otherwise they are free to re-interpret / re-order your code as they see fit. Also the micro-processor may do its own instruction re-ordering.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: which loop is faster?

    One more thing. Before you jump into testing which of the loops is faster, be aware that the compiler might actually optimize away the whole loop, as looping over doing doing nothing is the same as doing nothing. At least with small fixed numbers of loop iterations, "unrolling" those loops is a quite common optimization.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    Re: which loop is faster?

    I understand the question is somewhat pointless, i guess what i was asking is, when the compiler translates the C++ loops down into the machine language, is the resulting machine language the same for each loop or do they differ? Which you answered this.
    You should be able to set your compiler to generate a file with the assembler output it produces when it compiles a file. Obviously it requires some knowledge of asm to understand what's going on, but if your compiler can include the original C++ source code as comments in the asm, it's not terribly hard to figure out.

    In this case, MSVC9 compiling with /O2 generates identical asm for both loops, something like

    Code:
    xor esi esi
    $LL2@main:
    
    ; code that deals with "i", to avoid having it optimized out
    
    inc esi
    cmp esi, 1000000
    jl SHORT $LL2@main

  8. #8
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: which loop is faster?

    Wow that is an interesting feature( writing the assembly to file) ill have to check that out. thanks. I am kind of used to Java where i can actually see the .class file which is some sort of assembly language, right? But for C++ all i get is my .cpp file and the .exe.
    Last edited by g.eckert; February 27th, 2009 at 01:43 PM.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: which loop is faster?

    Quote Originally Posted by g.eckert View Post
    Wow that is an interesting feature( writing the assembly to file) ill have to check that out. thanks. I am kind of used to Java where i can actually see the .class file which is some sort of assembly language, right?
    That's the difference between a compiled language and Java, and the basic reason why I stated that it is kind of pointless to really think one loop is faster than the other.

    In Java, you have the class files that when created, produce only one way to do a for() loop, while() loop, etc.. That's why Java class files can be "decompiled" back into Java source code, regardless of what platform the Java class file was created on. Each Java language construct has a "template" of how it must look in the class file, so that the JVM, regardless of platform, can interpret the class file.

    This is not the case for a true compiled language. There is no determination as to what two pieces of similar code will boil down to at the object code level, since every compiler vendor has their way of optimizing (or not optimizing) the code. As long as the code works as expected, that's all that counts.

    When there were compiler wars going on a decade ago, you would constantly see computer ads saying "our compiler produces faster code than their compiler" with benchmarks to prove the advertiser's claim. It's the optimization that occurs within each compiler, given the same source code, that determines which piece of code will be faster.

    The basic thing is that attempting to micromanage small, inconsequential things such as a speed of a for() over a while() loop, speed of an if/else over a switch(), or some other minor difference in two sets of syntax, is IMO (unless it's for academic or some really specific reason) a waste of time when it comes to C++. What counts are the big things -- a certain algorithm over another, inordinate usage of the free store (which can slow down an application), passing large objects by value where the object has an expensive copy operation, etc.
    But for C++ all i get is my .cpp file and the .exe.
    You also have the object files, which the compiler produces. The linker takes the object files and creates the final executable from them.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 27th, 2009 at 02:40 PM.

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: which loop is faster?

    Quote Originally Posted by g.eckert View Post
    But for C++ all i get is my .cpp file and the .exe.
    I believe most compilers allow you to create the intermediate files. Turning a .cpp int a .exe involves several steps:
    - Preprocessor
    - Compiler
    - Assembler
    - Linker
    Have a read through your compiler documentation (for gcc, check the -E, -c and -S option) to see how you can have a look at intermediate files.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  11. #11
    Join Date
    Nov 2003
    Posts
    1,405

    Re: which loop is faster?

    Quote Originally Posted by Paul McKenzie View Post
    In Java, you have the class files that when created, produce only one way to do a for() loop, while() loop, etc.. That's why Java class files can be "decompiled" back into Java source code, regardless of what platform the Java class file was created on. Each Java language construct has a "template" of how it must look in the class file, so that the JVM, regardless of platform, can interpret the class file.
    This is complete nonsense.

    There are no "templates" a Java compiler must use when it lays down bytecode (the assembly language of the JVM). A Java compiler is free to use the available bytecode instructions anyway it likes to implement Java language constructs. It must follow the class file layout but that's another story.

    And although JVM's are interpreters most JVM's today use compilation to native code in the interpretation process at runtime. So in effect Java is a compiled language to native machine language. The runtime compilation isn't required to lay down code according to some "templates" either.

    So in no step of the Java compilation process (statically and dynamically) are there any "templates" that must be used.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: which loop is faster?

    Quote Originally Posted by _uj View Post
    A Java compiler is free to use the available bytecode instructions anyway it likes to implement Java language constructs. It must follow the class file layout but that's another story.
    I am strictly speaking at the class level. Of course the code has to eventually run on the CPU that's operating.

    The difference is that the optimizations done in Java and C++ are in two different areas -- that was my point. The optimizations in C++ are done on the object code level. The equivalent or close to equivalent to object code in Java is the class file.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Nov 2003
    Posts
    1,405

    Re: which loop is faster?

    Quote Originally Posted by Paul McKenzie View Post
    I am strictly speaking at the class level. Of course the code has to eventually run on the CPU that's operating.

    The difference is that the optimizations done in Java and C++ are in two different areas -- that was my point. The optimizations in C++ are done on the object code level. The equivalent or close to equivalent to object code in Java is the class file.
    Well you claimed this (about a "true" compiled language),

    "There is no determination as to what two pieces of similar code will boil down to at the object code level, since every compiler vendor has their way of optimizing (or not optimizing) the code. As long as the code works as expected, that's all that counts."

    The above is true also for Java compiled to bytecode. Java doesn't specify that the bytecode for say a for-loop or a while-loop must look a certain way. Different compiler vendors can do what they like including optimizations (as long as the result behaves according to the language specification).

    What you said about Java just isn't true. There are no "templates" specified for how a compiler must lay down bytecode.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: which loop is faster?

    Quote Originally Posted by _uj View Post
    The above is true also for Java compiled to bytecode. Java doesn't specify that the bytecode for say a for-loop or a while-loop must look a certain way. Different compiler vendors can do what they like including optimizations
    By "compiler", are you refering to javac? Isn't there only one vendor (Sun)? If there is more than one vendor, I stand corrected

    (his isn't the Java forum, so I don't want to keep this up.)

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Nov 2003
    Posts
    1,405

    Re: which loop is faster?

    Quote Originally Posted by Paul McKenzie View Post
    By "compiler", are you refering to javac? Isn't there only one vendor (Sun)? If there is more than one vendor, I stand corrected

    (his isn't the Java forum, so I don't want to keep this up.)
    There are more than one Java compiler vendor. IBM is one example. And Eclipse has it's own built-in Java compiler. But you would be wrong even if there were just one supplier of Java compilers. This is because also a Java compiler from the same vendor can differ between versions.

    Well, this isn't a Java forum but that doesn't mean you can spread bullshit about Java without being corrected.

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