Skip to main content
gamedev.net
🔒 Locked

Performance static vs member variable

Started by dansteph 2 replies 350+ views
Original Post
dansteph
dansteph
writing a simulator I face litterally thousand of variable holding them as variable member of a class bother me while my code will become difficult to read. So is there any performance issue to keep all the temp value as static into function instead of having them member of the class ? I have read also somewhere that making them static can increase performance. ? Dan
burp
burp
dansteph

...thousand of variable...

Are you sure, you have your class hierarchies set up correctly, that seems like an awful lot of variables for just one class. Check your "hasa" & "isa" relationships again.

...keep all the temp value as static into function...

I don''t understand why you would keep temp variable as members of a class, perhaps you could explain the problem a little clearer.

...I have read also somewhere that making them static can increase performance...

In asm a static variable is loaded like this "mov eax,[static address]", a member variable would be loaded like this "mov eax,[base+offset]". They both take the same number of cycles to execute.

I have noticed that the MSVC++ 6.0 compiler does a better job of optimizing member variables.
Stoffel
Stoffel
There are consequences of making a variable member or static (doesn''t really matter if by static you mean function, file or class-static). If they''re static, you are no longer reentrant. This is a significant design decision, and you''d best be certain it''s what you want if you choose to go this way.

I don''t believe there''s any speed difference. Using some test code, modifying the variables requires a dereference of an address; in the static case it''s an absolute address, and in the member case it''s an offset from the _this pointer. They''re both "mov"s, so I think they''re identical (though I''m by no means an ASM expert anymore--used to be in the 8086 days).
dansteph
dansteph
to burp: I didn''t mean in only one class but in the whole game
and yes the var count is hugh :-)

Thanks for the reply both

Dan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.