简体   繁体   中英

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?

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.

In worst case scenario you would exchange one goto for three logic condition checks so yeah - that would be considerably slower for you.

Having said that, I have to mention that "else if" mentioned by InBetween is the proper way of solving this issue. Looks much more clean than goto offering identical performance.

The "else if " solution is fine, but I feel that there are some misconceptions about how "if" affects performance. The real cost of "if" is not in the logical comparisons. It is in how it affects the CPU's branch prediction. A failed branch prediction disrupts the pipeline and would waste tens of cycles. Branch prediction is very dynamic and depends on how stable those logical values are. So in theory "goto" and also "else if" will be faster than an additional "if" statement. But by how much nobody knows. It depends on how those logical values change in runtime. Run some profiling if you really want to know. But when you do run your profiling, make sure those values change in the same pattern that they would in real use cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM