1

So I have a math class which performs calculations on the getters of a number of properties. Now I converted these properties so that after their first evaluation return only that result from then on.

Now concerning performance I was wondering that given that there will most likely be hundreds if not thousands of instances of these stored in collections, which approach is best on average?

Things that concern me is that the latter approach:

increases the GC load and it also affects locality of reference as nullables will allocate memory away from parent object.

public class SomeMathType
{
     // these are structs
     public Vector A { get; }
     public Vector B { get; }
     ...
     public double Length => A + B;
     ...
}

vs.

 public class SomeMathType
 {
      ...
     private double? _length;
     private Vector? _vector;

     // these are structs
     public Vector A { get; }
     public Vector B { get; }

     public Vector Vector
     {
         get
         {
             _vector = _vector ?? B - A;
             return _vector.Value;
         }
     }

     public double Length
     {
         get
         {
             _length = _length ?? Vector.Length;
             return _length.Value;
         }
     }
      ...
 }
zman
  • 333
  • 1
  • 2
  • 9
  • 5
    [Run your horses](https://ericlippert.com/2012/12/17/performance-rant/) – Equalsk Oct 24 '17 at 10:41
  • naive performance profiling will not establish anything. It's a complicated metric as it depends on how frequently these properties are accessed vs how expensive the issues I listed are... But yeah perhaps the difference it isn't relevant and it would be best to go with the more readable option. I was just wondering what others have to say about it... – zman Oct 24 '17 at 10:44
  • Why not calculate resulting vector in the constructor and make it non nullable? – Alex Sikilinda Oct 24 '17 at 10:47
  • Because then that cost will always occur up front upon construction. There are potentially many such calculations, and perhaps they will never be required. – zman Oct 24 '17 at 10:48
  • 1
    I don't see how `Nullable` would cause boxing, except over the *single* null value. – spender Oct 24 '17 at 10:58
  • 1
    `since it now boxes the structs into nullable reference types`, but `Nullable` is struct itself, not reference type. – Evk Oct 24 '17 at 11:02
  • 2
    Write your code for readability, not for performance. Good code convey the algorithm it implement as clearly as possible. Use meaningful names for variables, properties, members and methods. If you have a performance issue, profile it, find the bottle necks and fix them. – Zohar Peled Oct 24 '17 at 11:08
  • I see premature optimization here, which is the root of all evil. Just do not optimize it until you have problems in this specific part of application. – Yeldar Kurmangaliyev Oct 24 '17 at 11:47
  • @EVk, you're right nullables are structs as well, my bad. Always just assumed that they were reference types. – zman Oct 24 '17 at 11:49
  • Ok I see there is a strong trend in favour of making it more readable vs more performant – zman Oct 24 '17 at 11:50
  • If it's "a complicated metric", it's up to the user of this class to profile and see what's what, and they'll have a much easier job optimizing code that's obvious. If you really want to know what's best "on average", then define "the average" case and microbenchmark that. Be aware that you'll probably get it *wrong*, though. Performance problems are almost never where you initially *think* they'll be, hence the warning against wasting time and effort prematurely optimizing things. Never optimize based on ideas but no data at all; your intuition will fail you. – Jeroen Mostert Oct 24 '17 at 11:54
  • I wouldn't waste a second on this. Most likely this won't even be a performance bottle neck in production code and in the corner cases where it could be you can either: profile that case and decide the best strategy to improve performance or simply pass the burden to the consumer; let him cache the value in a local if it really is a bottle neck in some tight loop or something. – InBetween Oct 24 '17 at 12:00

1 Answers1

0

So it appears that the general consensus is to favor readability over (prematurely) performance optimized code. So I have kept the code it was originally written, at least for now.

zman
  • 333
  • 1
  • 2
  • 9