For loops and system memory

This is a discussion on For loops and system memory within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a question about those code snippets below. Sample A: Code: for(int i=0; i<getSomeLenght(); i++) { // Some ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23

    For loops and system memory

    Hello,

    I have a question about those code snippets below.

    Sample A:
    Code:
    for(int i=0; i<getSomeLenght(); i++) {
        // Some code here
    }
    Sample B:
    Code:
    int len = getSomeLenght();
    
    for(int i=0; i<len; i++) {
        // Some code here
    }
    Is Sample A presumably occupying more memory resources than Sample B? Or is there any difference at all?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    i dont know the true technicalities but i would say yes - if the for loop repeatedly calls the function to check the value - but perhaps it compiles in a static way, once the value is known then it is not reassessed - in which case it would be very slightly more expensive in the second example. I am interested to hear a correct answer on this also

    EDIT: In fact a loop must constantly check the value? Many times i have written stuff that changes the limit on the loop, within that loop itself. Or does that check only happen if the limit changes?
    Last edited by rogster001; 12-15-2012 at 04:36 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Actually as far as memory is concerned Sample B would probably use more memory for the extra variable. In Sample B the loop variable could be handled in a register.

    The bigger problem would be that Sample A might have to call the function every iteration, Sample B just once.

    As to the question as to whether or not the function will be called every time, depends largely upon what the function is actually doing and possibly compiler optimization levels. Worst case the function is called every time, best case only once, but this is really very compiler specific.

    Jim

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by heinz55
    Is Sample A presumably occupying more memory resources than Sample B? Or is there any difference at all?
    Maybe getSomeLenght is a function-style macro that evaluates to 0. The compiler detects this and eliminates the entire for loop, and also eliminates len in sample B when it finds that len is not used at all otherwise. Thus, no space is used at all in both samples.

    The way I see it, the correct answer to your question is: it depends. It depends on what exactly is your code. It depends on your compiler and options used. It may depend on other things too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    I think the correct answer is: it doesn't matter. It's a micro-optimization in that sense, and premature optimization is the Root of much Evil™.
    Is the function going to be called on every loop? It might be. But unless you know it's a tight loop, don't worry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Elysia
    I think the correct answer is: it doesn't matter. It's a micro-optimization in that sense, and premature optimization is the Root of much Evil™.
    Is the function going to be called on every loop? It might be. But unless you know it's a tight loop, don't worry.
    No, that is a correct answer, but it is not necessarily the correct answer, because we have no context to determine if this is a bottle neck where somehow such a micro-optimisation makes sense.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    I would counter-argue that precisely because we do not know that we should say that it doesn't matter. In 99% of the cases, it will not matter, especially in regards to the original question which asked if one would use more memory.
    If we started optimizing stuff because we didn't know if micro-optimizations made sense, then we would contribute to all that Evil™ that Knuth found
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Elysia
    I would counter-argue that precisely because we do not know that we should say that it doesn't matter.
    No, then you should state that:
    Quote Originally Posted by Elysia
    In 99% of the cases, it will not matter, especially in regards to the original question which asked if one would use more memory.
    Otherwise, you would be jumping to a conclusion, however probable it might be.

    Quote Originally Posted by Elysia
    If we started optimizing stuff because we didn't know if micro-optimizations made sense, then we would contribute to all that Evil™ that Knuth found
    Determining if something is an optimization and actually applying such an optimization are two different things.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The term you are looking for is if the function will be inlined. For example if you have

    Code:
    void getSomeLenght()
    {
        return length;
    }
    then the compiler might just replace it with "length" or "this->length" if you are in a class. If you don't have this then the code is not necessarily equivelent. If you have

    Code:
    void getSomeLenght()
    {
       if (length > 10000
          return 0
        else
          return length;
    }
    then doing "int len = getSomeLength()" is just something different. That been said, the compiler can indeed optimize if in the specific context getSomeLength() will always return the same value as "len" would. In this case, it really depends and it will depend again if the code is changed later on.

    If you want to optimize as Elysia said then it is advised to do so at a later stage and if you really want to optimize. So if you use the first piece of code and you want to "help" the compiler with the inlining and later you change the getSomeLength() function to the second piece of code, you will get an unexpected behavior. The compiler can still inline the second piece of code and make the check all the time and avoid the function call so your attempt to help the compiler might just have been in vain. All of this, of course, doesn't apply if you don't want to change the function, in which case helping the compiler is a way to optimize.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    If getSomeLenght [sic] is a constant time operation, whose results are not affected by the loop body, and it has no side-effects, then Sample B is probably premature optimisation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A C++ Help System for loops
    By sloppyjoe69 in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2011, 08:07 PM
  2. case, menu system, loops infinitely on invalid input
    By ModeSix in forum C Programming
    Replies: 12
    Last Post: 05-08-2011, 04:12 PM
  3. Designing a memory system
    By John_L in forum Tech Board
    Replies: 11
    Last Post: 10-01-2008, 04:42 PM
  4. Virtual Memory system
    By sicstus in forum C Programming
    Replies: 1
    Last Post: 05-20-2002, 04:16 PM
  5. System memory size?
    By dark_fusion in forum C Programming
    Replies: 3
    Last Post: 02-03-2002, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21