Performance of Public vs Private with accessors?

I wanted to get a sanity check on what the performance benefit would be of changing a private array with an accessor to a public array. This in a loop and I am processing LOTS of data. I figured I would some perf because the execution does not have to go through the accessor but I am not sure if Unity or C# that is somehow cached.

As long as I know public or private access is just to determine at compile time if you have access to a variable or not. So at runtime they are the same. There is no overhead of having a specific accessor over the speed of execution or something.

“Obviously,” the overhead of a function call makes an accessor slightly slower. But many compilers automatically inLine as needed, making accessor vs. public var the same.

In other words, it you have float A(int i) { get { return _A*; }} and the call A[4] elsewhere, it will be converted into simply _A[4] in the compiled code.*
But, agree with gjf, you might be spending time looking for a 0.1% increase from this, when a better algorithm, etc… would net you a 500% speed-up.

Thanks for all the feedback peeps. I was checking mainly for my own habits but the case in point is accessing sample data from an audio file. We have a playhead int in one class and the float array in other classes. I am processing several thousand times a tick. It seems like I am not going to get much perf out of making the array public but if there is no drawback, then I will do it anyway. It does not really affect my readability. I was just wondering if there was any magic in the accessor that might have made things faster. Its always good to check because my assumptions have been wrong before :slight_smile: