D
DrOrbit
I am working on a rather large program that hits several methods
multiple times. In looking for ways to speed it up, I have discovered
something odd. The first time I call a method, it's about 30 times
slower than any subsequent call. This is true for all the methods that
I have tested. For example, the first call might take 30 milliseconds
whereas all subsequent calls take less than 1 ms. I would like to
understand why this is happening.
An example method looks like this:
public void computeVecP () // Escobal 5.3
{
VecP.X = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.CosLongAsc) -
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinLongAsc * SatObs.Sat.KepElements.CosInc);
VecP.Y = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.SinLongAsc) +
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.CosLongAsc * SatObs.Sat.KepElements.CosInc);
VecP.Z = SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinInc;
}
Each term is "a property of a property of a property" and all values
have been pre-computed. In other words,
"SatObs.Sat.KepElements.CosArgPerigee" is declared as
public double CosArgPerigee{get{return cosArgPerigee;}
set{cosArgPerigee=value;}}
If I substitute simple variables (i.e. "X" for
"SatObs.Sat.KepElements.CosArgPerigee") then the time difference
disappears. Therefore I conclude that C# is spending a long time
locating the first property look-up but somehow doesn't need to do it
again for subsequent calls. I hope that I'm making sense here. Is
there a better or more complete explanation?
multiple times. In looking for ways to speed it up, I have discovered
something odd. The first time I call a method, it's about 30 times
slower than any subsequent call. This is true for all the methods that
I have tested. For example, the first call might take 30 milliseconds
whereas all subsequent calls take less than 1 ms. I would like to
understand why this is happening.
An example method looks like this:
public void computeVecP () // Escobal 5.3
{
VecP.X = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.CosLongAsc) -
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinLongAsc * SatObs.Sat.KepElements.CosInc);
VecP.Y = (SatObs.Sat.KepElements.CosArgPerigee *
SatObs.Sat.KepElements.SinLongAsc) +
(SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.CosLongAsc * SatObs.Sat.KepElements.CosInc);
VecP.Z = SatObs.Sat.KepElements.SinArgPerigee *
SatObs.Sat.KepElements.SinInc;
}
Each term is "a property of a property of a property" and all values
have been pre-computed. In other words,
"SatObs.Sat.KepElements.CosArgPerigee" is declared as
public double CosArgPerigee{get{return cosArgPerigee;}
set{cosArgPerigee=value;}}
If I substitute simple variables (i.e. "X" for
"SatObs.Sat.KepElements.CosArgPerigee") then the time difference
disappears. Therefore I conclude that C# is spending a long time
locating the first property look-up but somehow doesn't need to do it
again for subsequent calls. I hope that I'm making sense here. Is
there a better or more complete explanation?