XNA Game Development Forums
2012/05/18 07:24:59 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Properties VS public access  (Read 1548 times)
nicknz
Newbie
*
Offline Offline

Posts: 6


View Profile
« on: 2007/04/20 11:51:32 »

Hello:

I want to know what is better for performance of the game engine, the use of properties or the use of public access data. I know the properties are needed to keep a good OO design, but in a game engine I think that performance is more important. Does the use of properties add a little overhead to the engine? Does public access is faster?

Thanx

Nick
« Last Edit: 2007/04/20 11:54:27 by nicknz » Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #1 on: 2007/04/26 13:51:28 »

Not sure, but personally I would use objects.

As your engine progresses and becomes more complex, you'll kick yourself in the head if you try and store everything as global variables.

Plus, as you said, it's bad design...
Logged
muchrejoicing
Newbie
*
Offline Offline

Posts: 46


View Profile
« Reply #2 on: 2007/04/26 18:19:54 »

I think the question was about public properties vs. public variables in objects -- as in

Code:
public class Example {
    public Vector3 Position;
    // versus
    private Vector3 myPosition;
    public Vector3 PositionAccessor { get { return myPosition; } set { myPosition = value; } }
}

I know nothing about how C# works, so my instinct is to say that public accessors act like a getPosition() accessor method, while public variables are smart enough to stay off the stack. Can anyone clear this up?

My general approach is to use public variables unless I absolutely need accessors (to only allow a get, or to manipulate other fields when one is set).
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/04/27 01:32:31 »

Personally I prefer properties, as you can give a property behaviors that a simple field can not do, also you can override this behavior in any classes that inherit from the base. You can also restrict access to a field by only having get or a set accessors in it's body.

As far as speed goes then I "imagine" that the direct access to the field will be faster as you are not going through a property layer, but again it depends on what you want to use this field for in your object.

So, you will probably end up using a mixture of the two depending on what you want to use the variables for.
Logged
Chr0n1x
Global Moderator
Sr. Member
*****
Offline Offline

Posts: 307


View Profile WWW
« Reply #4 on: 2007/04/27 08:42:36 »

It also depends on what the compiler does to optimise this stuff. Best bet is to pose this question to the Microsoft guys who would be able to pass this on to someone who knows the details.
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.099 seconds with 17 queries.