local stack variables

This is a discussion on local stack variables within the C++ Programming forums, part of the General Programming Boards category; I would guess the instruction inside the loop is being optimized out since it doesn't result in anything....

  1. #16
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I would guess the instruction inside the loop is being optimized out since it doesn't result in anything.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  2. #17
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    woah woah, actually debug mode initializes the values in all cases where its not initialized. Invalid test.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Not exactly.... The debugger tries to remove anything that will result in undefined behavior in a way that is still undefined while easier to spot as a programmer. It actually makes spotting errors so much easier.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by FillYourBrain View Post
    I would guess the instruction inside the loop is being optimized out since it doesn't result in anything.
    It isn't, I verified with assembly.

    Quote Originally Posted by FillYourBrain View Post
    woah woah, actually debug mode initializes the values in all cases where its not initialized. Invalid test.
    Not so. They're not always initialized. And if they were, it would mean it's being written twice in the second loop.
    But I agree that it would be better with Release (I just had trouble convincing the compiler to stop optimizing the entire code away).

    Here's another version that can be compiled in Release:
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	DWORD dwTick1 = GetTickCount();
    	for (int i = 0; i < 100; i++)
    	{
    		char x[100000];
    		strcpy(x, "This is a test\n");
    		cout << x;
    	}
    	DWORD dwTick2 = GetTickCount();
    
    	DWORD dwTick3 = GetTickCount();
    	for (int i = 0; i < 100; i++)
    	{
    		char x[100000] = {};
    		strcpy(x, "This is a test\n");
    		cout << x;
    	}
    	cout << "Took " << dwTick2 - dwTick1 << " ms.\n";
    	cout << "Took " << GetTickCount() - dwTick3 << " ms.\n";
    }
    The amazing results I get from this are:

    Took 203 ms.
    Took 188 ms.

    Took 234 ms.
    Took 235 ms.

    Took 281 ms.
    Took 281 ms.

    Took 172 ms.
    Took 375 ms.

    Took 187 ms.
    Took 234 ms.

    Took 375 ms.
    Took 250 ms.

    These results can be unpredictable and not 100% accurate due to the nasty work of the scheduler, but it shows that they take about roughly the same amount of time.
    It's not quite as expensive as you would have everyone believe.
    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.

  5. #20
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by master5001 View Post
    Not exactly.... The debugger tries to remove anything that will result in undefined behavior in a way that is still undefined while easier to spot as a programmer. It actually makes spotting errors so much easier.
    What I meant was that it initializes to some value... not zero. I've seen visual studio debug mode with numega bounds checker initializing to dead beef... hex 0xDEADBEEF.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #21
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by Elysia View Post
    It's not quite as expensive as you would have everyone believe.
    Wasn't my premise.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by FillYourBrain View Post
    Wasn't my premise.
    Was directed at master5001, not you. Sorry.
    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. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes... but it is. I base this on experience with clearing out stack space in an embedded system. If you would like I could certainly continue to dispute your "infallible" test program more. Or you can just accept that this is one of those stalemates we always end up at after dozens of posts back and forth with no change in opinion in sight.

  9. #24
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It indeed seems that in debug mode looking after uninitialized things is more expensive. In optimized release mode the uninitialized version is slightly faster (and it doesn't seem that MingW optimizes anything out).

    Took 4485 ms.
    Took 4562 ms.

    However the stack is usually not just one variable. Try replacing x with x[10] and 0 with {0}.

    Took 4485 ms.
    Took 33921 ms.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh.. I took it as being directed at me. Not all processors use out of order execution, by the way. Which is one reason that your zeroing may seem completely benign.

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Of course, some systems may not be equal in terms of performance for this test. It is by no means a guarantee that it isn't expensive and not real proof, but it is a small test that proves that zeroing may not be as expensive as you claim, at least not on all platforms.
    It varies from system to system, but on PCs, I can pretty certainly say it's viable to do so without a big overhead. It's just a matter of the OS builders not choosing to "do so."
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Finished Stack
    By the pooper in forum C Programming
    Replies: 11
    Last Post: 02-02-2005, 09:52 AM
  3. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  4. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 09:59 AM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM

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