0

For example, if I have a loop

for(int i = 0; i < N; i++) {
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    foo(a, b, c, d, e);
}

or

int a, b, c, d, e;
for(int i = 0; i < N; i++) {
    cin >> a >> b >> c >> d >> e;
    foo(a, b, c, d, e);
}

which one should be faster? In the first case, I define the five variables inside the for loop, and in the second one, I define it outside.

I've seen posts that talk about "which is faster" such as Difference between declaring variables before or in loop?, but I'm not sure which one takes less memory.

I don't really care about the complexity, but rather the amount of memory used up in my program.

Obviously, in this case, it doesn't really matter, but what if I have a multi-dimensional for-loop and I have to define variables millions of times? Or, what if I define a large vector with many elements inside multiple times?

I apologize if this question is really simple, as I am new to c++. Any help would be greatly appreciated. Thanks guys.

  • 1
    What did you observe when you tested the above code? Did you notice your program using more memory in either case? – Tas Nov 29 '17 at 04:26
  • 1
    I'd say it depends on how the compiler handles it, but the second way may be better if it's un-optimized. – Spencer Wieczorek Nov 29 '17 at 04:26
  • I'm not sure how to check the amount of memory my program uses. I'm doing contests, and they have a memory limit, so I'm just wondering because I use an online compiler that doesn't tell me. –  Nov 29 '17 at 04:27
  • 2
    In the first code snippet, you will wind up allocating five ints on the stack in each loop iteration, then popping them off at the end -- this takes slightly longer. But to @SpencerWieczorek's point, the optimizer in a real world situation will handle this for you. – MrEricSir Nov 29 '17 at 04:30
  • 1
    @bobjoe628 The first way doesn't really use more memory, rather it just has an unnecessary steps for performance of having to remove and add those variables on each iteration. (*That's assuming the compiler doesn't handle it that is*) – Spencer Wieczorek Nov 29 '17 at 04:31
  • Oh, okay...what about the post that I linked? Sorry to bother you guys, but just as a side question, is this question acceptable as an okay question in SO, because I'm new here, and I"m not sure what defines a good question... –  Nov 29 '17 at 04:31
  • 1
    @bobjoe628 What about it? The top answer is saying basically the same thing here, that the compiler will likely take care of it anyways. – Spencer Wieczorek Nov 29 '17 at 04:34
  • Oh, okay nevermind, sorry. –  Nov 29 '17 at 04:35
  • @bobjoe628, Your linked question is for Java not C++ – asimes Nov 29 '17 at 04:59

2 Answers2

1

I copy / pasted your code into two files named foo.cpp and bar.cpp:

foo.cpp:

#include <iostream>

void foo(int a, int b, int c, int d, int e) {
        std::cout << a << b << c << d << e << std::endl;
}

int main(int argc, char** argv) {
        for (int i = 0; i < argc; i++) {
                int a, b, c, d, e;
                std::cin >> a >> b >> c >> d >> e;
                foo(a, b, c, d, e);
        }
        return 0;
}

bar.cpp:

#include <iostream>

void foo(int a, int b, int c, int d, int e) {
        std::cout << a << b << c << d << e << std::endl;
}

int main(int argc, char** argv) {
        int a, b, c, d, e;
        for (int i = 0; i < argc; i++) {
                std::cin >> a >> b >> c >> d >> e;
                foo(a, b, c, d, e);
        }
        return 0;
}

I compiled them with no optimization like this (with gcc version 6.3 if it matters):

g++ -O0 foo.cpp -o foo
g++ -O0 bar.cpp -o bar

Both results, foo and bar, were identical even with no optimization. To be certain of this, you can compare the objdumps:

objdump -D foo > foo.txt
objdump -D bar > bar.txt
diff foo.txt bar.txt

However, some things to point out:

  • int is a primitive type, if these were say instead instances of a class then perhaps there could be a difference (in performance, not memory usage). This would be because each iteration of the loop would create and destroy the instances if the compiler (without optimization) did not make the results the same
  • Your variables are taking memory on the stack (because you did not use new). Even in the case where the compiler was not able to produce the same result from the two codes you would be using the same amount of memory
  • If you were to instead create variables with new and not delete them when they were created inside the for loop then the variables would take space on the heap instead of the stack and then the first code would use more memory. There is no reason to do this but if you did you could still use the same amount of memory by calling delete at the end of each loop
asimes
  • 5,749
  • 5
  • 39
  • 76
0

No, each loop iteration will use the same memory. And with primitive types (such as int) if you don't initialize them you won't even suffer that penalty. If you were to do that with a type that did require initialization the second might well be faster (though if you were using such a type whether it were inside the loop or not would likely be determined by what you were doing with the instances).

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23