Mozzie Mozzie - 3 months ago 8
C# Question

Would using a goto statement be faster than letting an if statement return false?

Im writing some very complicated real time anlytical algorithms in c# and speed is key for the success of my program. If I am unable to keep 45 processes of the changing data a second then the PID control loop on the other end of the TCP server will oscillate. I could re-tune the PID loop to run at a slower "fps" but that is improbable. Just thought Ill tell you that before you write answers that say "the difference is only 10ms so nothing really" cause those 10ms is a lot to me.

Would if be faster to use a

goto
statment like so.

if (GeneralDataDirection && UseMaxCandleData || GeneralDataDirection && !UseMaxCandleData)
{
Y1 = CandleStickWorkingData.Min();
X1 = Array.IndexOf(CandleStickWorkingData, Y1);
goto FirstPointCalculated;
}

if (!GeneralDataDirection && UseMaxCandleData || !GeneralDataDirection && !UseMaxCandleData)
{
Y1 = CandleStickWorkingData.Max();
X1 = Array.IndexOf(CandleStickWorkingData, Y1);
}

FirstPointCalculated:


Or would it be faster to remove the
goto
statement from the first if clause and let the second if clause return false?

Answer

You don't need the goto at all, just use an else if:

if (GeneralDataDirection && UseMaxCandleData || GeneralDataDirection && !UseMaxCandleData)
{
     Y1 = CandleStickWorkingData.Min();
     X1 = Array.IndexOf(CandleStickWorkingData, Y1);
}
else if (!GeneralDataDirection && UseMaxCandleData || !GeneralDataDirection && !UseMaxCandleData)
{
     Y1 = CandleStickWorkingData.Max();
     X1 = Array.IndexOf(CandleStickWorkingData, Y1);
}

If the first condition is true the else if condition wont be evaluated and its essentially a goto already.

Comments