Search Unity

Are Public variables faster than private and how much?

Discussion in 'Scripting' started by AlanMattano, Sep 5, 2015.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Looking Unity Unite 2015 Europe videos a game developer claim that public are faster than private and ask to the public the reason.

    Are really Public variables faster than private?

    If it is ture, How much is the difference?
    Is there more info about this type of optimization?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    That's the first I ever heard of it, if so.

    It's probably faster to develop if you use all public variables. That way you don't have to bother with encapsulation and keeping your code neat and, well... correct.

    Don't fall into the trap of over optimization, especially prematurely. If you're to the point where you're trying to get performance boosts by modifying your variable access type, chances are your problems are plenty, and your time would be better spent solving those issues than something as minor as speed of public vs private.
     
  3. jgnmoose

    jgnmoose

    Joined:
    Apr 1, 2014
    Posts:
    44
    Whatever difference there may be is too small to worry about. You would be better off finding out what functions are taking the most time and trying to optimize those instead.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No. As far as I am aware there is no difference. If there is a difference it's not important enough to care about.

    No. What you save in writing code you will loose in refractoring and debugging. We don't code neat and correct for the fun of it. Good code structure and encapsulation save a lot of time.

    Can you reference the exact video? I'd be curious to see the context.
     
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    63 Video! 45min media each... I remember that the game developer was making a lot of optimizations for making work the gamne (phone game? ) and he discover this difference in performance. He was expecting that private was more performer than public. But what he discover is that public are faster than private. So he was asking to the audience and to Unity why. If he change his code in the optimization process, means that he was not loosing his time. But he did not mention how much was the difference. Probably was a phone target. If you find the video please put the link.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    public fields are faster than a public property that accesses a private field.

    This might be what they were talking about.

    Fields beat properties hands down, because a property incurs an extra function call (since properties are actually a special kind of paired getter/setter functions), and also because field access allows direct access to the memory location of structs, where as a property returns copies of the struct.

    But public fields vs private fields, there isn't any difference. The access modifier is more a compile time constraint. It's use is for conveying what level of access code from other sections of the program have to this section of memory. It helps me convey to you when we share code with one another what you should and shouldn't be doing to the objects in our joint project (theoretically).

    But once compiled and ran, the entire program has free access. The imposition of access level is merely superfluous to the actual program... this can be seen from the developing side thru the fact that internal/private/protected members can all be reflected just as easily as public members.



    With all that said... unless you're doing some bleeding edge stuff that requires as much juice as you can pump out of your code... you don't really need to be doing any of this.

    Especially not in unity, since really, if you're that bleeding edge, you shouldn't be running in a JIT runtime like Mono/.Net.

    Things like garbage collection are a much bigger concern, and even then, not as much as some make it out to be.

    In the RARE cases where this might impact you (maybe you have to read and write the fields of thousands of objects in a single frame)... then make these minor optimizations in this one spot.

    Otherwise... encapsulation and the sort save you development and maintenance time in so many more ways.
     
    Last edited: Sep 7, 2015
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Last I heard it was the opposite (private over public) - not that I can see how, other than affecting the editor itself, which is about as important as ... your mother in laws opinion (not very - lol)

    Lord makes good points about properties over fields though.
     
    Timelog and Kiwasi like this.
  8. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501

    Ok , You are right. I do not want to make confusion here or confuse readers. I take my time to look for the video to clarify.

    The video link:


    So my question : "Are Public variables faster than private and how much?" is wrong. Because is not in the context of the video.
    Probably there is a better question, that I'm not able to formulate since I'm a Jr. in C# but is similar to this:

    "Are Public field faster than private [SerializeField] and how much?"
     
  9. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    In Unity, I think public fields might actually be slower than private ones, because of the automatic serialization that happens for them to show up in the Inspector.

    It might be the same overhead as using [SerializeField] on non-public fields I reckon... But for the unaware, it might actually make a difference.

    Declaring everything public just to avoid having to deal with access to members in other objects (apart from being terrible coding practice), will probably have an effect on performance in the long run... Just how much, I have no idea, but it's certainly one more reason to not 'default' everything to public.

    * Everything private by default
    * Raise access when needed (or provide accessors instead)
    * Minimize class coupling
    * Quality of life improved!

    Cheers
     
    honor0102 likes this.