Code optimization

Sep 20, 2013 at 10:25 PM
First at all, my english isn't good, so I hope I can explain well.

Looking into the source code of the WPF Instrument Cluster App I found that the "needle" of each gauge is refresh every 30 milliseconds and each time performs a rotate transformation, even if the final angle was reached.

To avoid above situation, I added a simple condition to determine if the rotation is necessary or not. Avoiding the constant calculation of the rotate transformation reduces the processor usage..

The modified code is in the uxSB_GameLoop_Completed function of the Needle class, this function handles the Completed event of the Storyboard. Below is the modified code in C#:
void uxSB_GameLoop_Completed(object sender, EventArgs e)
{
  if (Math.Abs(desiredAngle - Needle_RotateTransform.Angle) > 0.1)
  {
    Needle_RotateTransform.Angle += (desiredAngle - Needle_RotateTransform.Angle) / Math.Max(SLOWNESS, 1);
  }
  uxSB_GameLoop.Begin();
}
Test it and tell me if you found useful.
Coordinator
Sep 24, 2013 at 5:19 PM
Makes sense...good optimization. Thanks!