I’m trying to create a structure that’s essentially just a regular double
but has a physical unit attached via generics.
My attempts perform terribly, even without generics. Could somebody give me hints how I could make this faster, if possible at all in C#?
I’ve used
public readonly struct MyDouble
{
public readonly double Value;
public MyDouble(double value)
{
Value = value;
}
public static MyDouble operator +(MyDouble self, MyDouble value)
{
return new MyDouble(self.Value + value.Value);
}
public static MyDouble operator *(MyDouble self, double value)
{
return new MyDouble(self.Value * value);
}
}
and while mere constructing, storing, and retrieving these is as fast as for doubles, as soon as I use the operators, performance tanks compared to raw double arithmetic.
I reckoned this is just a matter of joining, but adding MethodImpl.AggressiveInlining attributes to the operators didn’t help at all.
I verified the poor performance using unit tests as well as using JetBrain’s dottrace on production runs.