• Advertisement

Archived

This topic is now archived and is closed to further replies.

Bringing lameness to a new degree.. c++ question.

This topic is 6217 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m wonder about something.. again.. a question probably so lame that seasoned programmers will develop unknown diseases just by reading it.. But i''m fairly new to c++. I wonder about speed-penalties concerning global vs. smaller scope variables. Lets say i have a DirectDraw engine, with a Draw () routine in the midst of it. In it, i use vars x, y, i and other common variables. Is it really bad to have those as global vars, apart from making my code more messy? My main point is. As it is now, i define all my vars in the draw () routine like this void draw () { int x, y, i, ypos; // locking surface and draw stuff // draw incredibly foxy effect // unlock surface } But is it really wise to re-define these variables every frame? Should i use static or some other prefix to make it more efficient? Or should i keep them global? I''m just wondering, please dont flame As i said, i''m new to this, but i''ve read somewhere that since i probably wont overwrite the memory which holds the variable, it will even retain its value the next time i call draw ()... Meaning that if x contains the value 120 after the first time i call draw (), it will still be 120 the next time, even if i define it again. Is this true aswell? I know this is a dangerous approach to trust, i''m just curious. I''m sure i could have asked this question using only 1 sentence, but hey, this is me defster

Share this post


Link to post
Share on other sites
Advertisement
Either make the variables global or pass them like so

void draw (int x, int y, int i, int ypos)

I recommend just keeping them global for most situations. Never define variables in a function unless you absolutly must.

Ben
http://therabbithole.redback.inficad.com
http://www.vendettaonline.net




Share this post


Link to post
Share on other sites
There''s nothing wrong with using a global variable in some cases. If it takes too long to init. this variable or it is used in (nearly) every time critical function, it is ok to make it global.

The problem with globals is that people over use them. They must reside in RAM until your program ends. Now, think about it, do you want this variable to take up RAM the entire run length of your program? Make sure that it is either going to either way, or must or face a speed hit before you make it global .

BTW: Games break "the rules" all the time to get extra speed. Just don''t break too many too often.



http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Its always nice to see how other people do it, but is there any actual speed-decrease in redefining vars every frame?

KalvinB says i could pass each variable as arguments to my function, but doesnt that just make it slower, since i use the stack so often? Ofcourse the "inline" command comes to mind, but still...

The same applies to my 3dengine, where i pass lots of variables to my DrawScanLine () function which interpolates x values, vertex values and z values and draws a shaded line in my polygon. This function is called several thousand times pr frame. Making all those vars global scares me, because it would make my code look very messy, besides alot of them are deleted since i add and remove 3d meshes during the main course of my program. For the record, i havent noticed any speed gain in using "inline" infront of that function, but i still use it "because i should"...

Share this post


Link to post
Share on other sites
AAAAAAAAAAIIIIIIIINNNNNNNNNNNNNTTTTTTTTT!

(That''s a ''wrong'' buzzer sound.)

Not only should globals be avoided for breach of object oriented programming, but they have a higher chance of being slower. I have seen that compilers are really nifty when it comes to local variables, and majority of the time they are kept in registers (no stack opperations); as opposed to a global always requiring a memory access. Even if a push is performed, two can be done in one cycle. You can help your compiler out by keeping all accesses to a variable close together, with minimal variables in between.

.travois.

Share this post


Link to post
Share on other sites
Moving the stack pointer & initializing a couple of DWORDs once per frame will have virtually no impact on the speed of the code. (Yes technically it does, ~30ticks, but the render loop is going to take at least 1000000 if it does almost nothing - you can afford at least 6000000 ticks per frame)

I assume that eventually you''ll need to have sprites on the screen, and each one will need their own x,y (along with a pile of other data) that will need to be passed to a draw function of some sort.

If you need them to retain thier value, make them static as Bishop_pass suggested.

Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post


Link to post
Share on other sites
You shouldn''t be worried about recreating a few extra ints every frame. The stack hit will be negligable. If you were recreating a giant structure, I''d pass it in as one of the parameters or something. With today''s computer speeds, I would write everything in a most readable format first, then optimize if you really see a need. What will happen is you will be worried about whether or not to make this object global because of speed considerations, and what really is bogging you down is your texture mapper needs to be rewritten in assembly. Or something like that.

Rule of Thumb 1: Don''t try to optimize too early.

And if you are already starting to optimize, I seriously doubt that is your hold up.

A more direct answer. Yes, it''s "possible" the memory for x won''t be over-written, but it''s also just as possible that there is now complete garbage, and probably more likely.

Global variables means you have to wrap your brain around more functions that could operate on that variable. It''s much easier to consider 1 function that could affect the variable as opposed to the 20 other functions that could access it if it were global.

Rule of Thumb 2: Make all variables local until it''s seen they need a larger scope. Then try classes/structures, and as a last resort use global.

Globals aren''t bad, they should just have limited use.

Ut

Share this post


Link to post
Share on other sites
There seems to be a misunderstanding about the way local variables are initialized. The compiler computes the total size, in bytes, of all local variables, and adjusts the local stack pointer (register ebp) by that amount. It is one adjustment per function, not one adjustment per variable, so it''s unimportant how many variables you have. I think. What I know for sure is that passing variables as parameters is slower than any other approach, because you must push each individual variable onto the stack, and then pop each variable off it.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites

  • Advertisement