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.
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.
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.
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)