1

I wasn't sure exactly how to word this question, but basically, I have a struct stNeuralLayers (for a neural network I'm playing around with) with fields that are matrices (such as a double[,] neuralWeights field and a double[,] results field), and I have a field that's an array of those structs (stNeuralLayers[] fldNeuralLayers).

The way I originally wrote it was that I had separate fields in the class (not in the struct) that were maintained separately such as int[] fldWeightsPerNeuron (which would store the number of weights per neuron for each layer as an array for the corresponding field) with the corresponding read-only property int[] numWeightsPerNeuron which would return a clone of the field.

The reason for this is because in a lot of places, I'm looping through the weights and accessing fldWeightsPerNeuron[i] in a statement like for (int j = 0; j < fldWeightPerNeuron[i]; j++) is I think faster, especially if your also looping through all the layers with for (int i = 0; i < numLayers; i++) (numLayers is also an int field that's maintained separately).

Obviously, the cost of this is the code required to maintain those fields when anything is updated, so I through around the idea of just getting rid of the fields and creating read-only properties that just fetch the needed data directly (such as putting public int weightsPerNeuron { get { return weights.GetLength(1); } } directly in the struct itself). The cost of doing that I think is if I use those properties directly in those for loop statements, it'l slow down performance (since accessing the GetLength() method each time is slower than accessing a single element of an array each time). I could also create temporary variables to store that data (such as int[] weightsPersNeuron = fldNeuralLayers.Select(x => x.weights.GetLength(1)).ToArray(); within the method for each method that would need it, but that's really just replacing one set of code (the code for maintaining separate fields) with another set of code (the code for declaring those variables and creating the arrays to assign to them within the various methods).

Is there a best practice for this?

Tara
  • 151
  • 3
  • 2
    Have you identified an actual performance problem in your code through profiling or measuring? – Robert Harvey Mar 17 '19 at 18:57
  • @RobertHarvey Not as of yet, I've only tested the network so far on a very small scale, but I'm trying to future proof. I'm not trying to solve a specific problem at the moment, I'm just looking for what's generally considered to be best practice. – Tara Mar 17 '19 at 19:08
  • @RobertHarvey I was thinking of going with the second method b/c not only does it eliminate maintenance code, but also removes potential points of failure as the properties are accessing the data directly rather than secondary fields that represent the data, but again, using the variable method just swaps out some code for other code, and I've never been a fan declaring redundant variables within methods when you can just access the data you need directly in-line. Even for complicated lines of code, rather than using variables, I use an extension method that mimics VB's "with" keyword. – Tara Mar 17 '19 at 19:13
  • 1
    Variables don't cost you anything in C#, in terms of performance (you were going to make that calculation anyway), and [they confer benefits on readability](https://softwareengineering.stackexchange.com/a/388438/1204). The effect on performance of properties is minimal in 99% of cases, and the benefits of using properties are compelling. So you'll have to figure out whether you're in that 1% that justifies public fields, and the way you do that is by measuring. – Robert Harvey Mar 17 '19 at 19:19

0 Answers0