S
Stephan Rose
In one of the applications I am developing I have need of working with
numbers in varying unit system. I might be adding 5 mil to 1 inch or
whatever.
What I have come up with works really well, however...it moves about
as fast as a honda civic trying to tow a semi truck.
The basic definition looks like this:
public struct ScalarGeneric : IScalar
{
private decimal value;
private ScalarUnit unit;
// Properties, constructors, operators, etc. skipped
}
Using decimal for precision and to avoid floating point issues.
The problem essentially is this that in my operator overloads for +,
-, etc. before I can perform the actual operation I need to obviously
check what unit space my first operand is, and then convert the second
operand to match the first, then perform the operation and return the
result in the matched unit space.
So what I end up with is, is something like this:
public static ScalarGeneric operator + (ScalarGeneric a, IScalar b)
{
switch(a.unit)
{
case ScalarUnit.Centimeter:
return new ScalarGeneric(a.Value +
b.ToCentimeters().Value, ScalarUnit.Centimeter); case
ScalarUnit.Inch:
return new ScalarGeneric(a.Value +
b.ToInch().Value, ScalarUnit.Inch);
case ScalarUnit.Mil:
return new ScalarGeneric(a.Value +
b.ToMil().Value, ScalarUnit.Mil);
case ScalarUnit.Millimeter:
return new ScalarGeneric(a.Value +
b.ToMillimeters().Value, ScalarUnit.Millimeter);
case ScalarUnit.Degree:
return new ScalarGeneric(a.Value +
b.ToDegree().Value, ScalarUnit.Degree);
case ScalarUnit.Radian:
return new ScalarGeneric(a.Value +
b.ToRadian().Value, ScalarUnit.Radian);
default:
throw new System.InvalidOperationException();
}
}
And so on for other operators.
Bottom line, this works really well but isn't all that fast. I have
been trying to find ways to optimize this as even a small improvement
would make a huge difference as I can easily end up with thousands of
ops when redrawing a full scene. So far though I have been drawing a
blank on how / if I can manage to get any additional performance out
of this.
If anyone has any suggestions I would love to see them!
Thanks,
--
Stephan
2003 Yamaha R6
kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara
numbers in varying unit system. I might be adding 5 mil to 1 inch or
whatever.
What I have come up with works really well, however...it moves about
as fast as a honda civic trying to tow a semi truck.
The basic definition looks like this:
public struct ScalarGeneric : IScalar
{
private decimal value;
private ScalarUnit unit;
// Properties, constructors, operators, etc. skipped
}
Using decimal for precision and to avoid floating point issues.
The problem essentially is this that in my operator overloads for +,
-, etc. before I can perform the actual operation I need to obviously
check what unit space my first operand is, and then convert the second
operand to match the first, then perform the operation and return the
result in the matched unit space.
So what I end up with is, is something like this:
public static ScalarGeneric operator + (ScalarGeneric a, IScalar b)
{
switch(a.unit)
{
case ScalarUnit.Centimeter:
return new ScalarGeneric(a.Value +
b.ToCentimeters().Value, ScalarUnit.Centimeter); case
ScalarUnit.Inch:
return new ScalarGeneric(a.Value +
b.ToInch().Value, ScalarUnit.Inch);
case ScalarUnit.Mil:
return new ScalarGeneric(a.Value +
b.ToMil().Value, ScalarUnit.Mil);
case ScalarUnit.Millimeter:
return new ScalarGeneric(a.Value +
b.ToMillimeters().Value, ScalarUnit.Millimeter);
case ScalarUnit.Degree:
return new ScalarGeneric(a.Value +
b.ToDegree().Value, ScalarUnit.Degree);
case ScalarUnit.Radian:
return new ScalarGeneric(a.Value +
b.ToRadian().Value, ScalarUnit.Radian);
default:
throw new System.InvalidOperationException();
}
}
And so on for other operators.
Bottom line, this works really well but isn't all that fast. I have
been trying to find ways to optimize this as even a small improvement
would make a huge difference as I can easily end up with thousands of
ops when redrawing a full scene. So far though I have been drawing a
blank on how / if I can manage to get any additional performance out
of this.
If anyone has any suggestions I would love to see them!
Thanks,
--
Stephan
2003 Yamaha R6
kimi no koto omoidasu hi
nante nai no wa
kimi no koto wasureta toki ga nai kara