Are Public variables faster than private and how much?

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?

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.

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.

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.

3 Likes

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.

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.

4 Likes

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.

2 Likes

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:

https://www.youtube.com/watch?v=4lAam8Marns

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?ā€

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

1 Like