Variable creation overhead?
Hello, I''m going to start on my first game but I never thought about this certain situation until now. It''s about variable created while inside hte main loop. Here, take a look:
int GameMain()
{
int start_map;
int end_map;
// Rest of code
return 1;
}
What I''m wondering is this: The main loop is going to be called many times, but each iteration it needs to create the variables again. Is this bad for speed issues (about 40 fps) and memory allocation? Should I have the variables global or perhaps static?
That brings me to another question... are static variables a bad practice, and are they common in games?
---
Help points: 1 (Eh... I have to start somewhere)
quote:Original post by Weston
What I''m wondering is this: The main loop is going to be called many times, but each iteration it needs to create the variables again. Is this bad for speed issues (about 40 fps) and memory allocation? Should I have the variables global or perhaps static?
That brings me to another question... are static variables a bad practice, and are they common in games?
Right now, those variables are going to be constantly allocated on the stack, so it isn''t going to be a big deal. I wouldn''t worry about the overhead of creating two int''s anyway.
Statics are common in games, and only a bad practice if you don''t understand the side effects of them (namely, variable initialization only occurs once [the first time a procedure is entered]).
RomSteady - Able to leap off tall buildings in a single bound
I think I know what I''m talking about here...but if I''m wrong, I''m sure that somebody else will straighten me out
Variables that you define this way are created on the stack. When you create them, the only thing that the compiler has to do is add how much space you need to the stack pointer. This is _not_ a big speed hit, and I don''t believe that you''d ever notice it, no matter how many variables you define this way. On the other hand, if you initialize each variable while you''re decarling it...then you could have a bit more of a speed hit, but again, I really doubt that you''d notice it too much.
Use global and static variables only if you need them. They increase the memory footprint your program has, plus, they''re just bad form.
Variables that you define this way are created on the stack. When you create them, the only thing that the compiler has to do is add how much space you need to the stack pointer. This is _not_ a big speed hit, and I don''t believe that you''d ever notice it, no matter how many variables you define this way. On the other hand, if you initialize each variable while you''re decarling it...then you could have a bit more of a speed hit, but again, I really doubt that you''d notice it too much.
Use global and static variables only if you need them. They increase the memory footprint your program has, plus, they''re just bad form.
Put the variables outside the main loop. No reason for them to be created every frame.
It wouldn't really slow the game down noticibly though or have an impact on memory. But put them out of the main loop anyway.
Oh and I don't think static variables are a bad practice. I haven't seen much code with them in but who cares? Code stuff your way not someone elses way
[edit]Damn some people are fast posters. Rom steady beat me too it[/edit]
[edited by - Monder on July 27, 2002 9:28:48 PM]
It wouldn't really slow the game down noticibly though or have an impact on memory. But put them out of the main loop anyway.
Oh and I don't think static variables are a bad practice. I haven't seen much code with them in but who cares? Code stuff your way not someone elses way
[edit]Damn some people are fast posters. Rom steady beat me too it[/edit]
[edited by - Monder on July 27, 2002 9:28:48 PM]
I''d leave them in there. The cost of creating those 2 variables is just that of moving the stack pointer up 8 bytes (implementation dependent, of course) which is negligible. I disagree with moving them out of the function because they are only relevant inside the function. Everything should be kept as local as possible, to make it easier to read and debug. Now, if these were not ints, and were files, or graphics, or something, then I would consider moving them as the overhead might be pretty high. But the general rule is to keep things local until you know for sure that it''s too expensive that way.
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement