dcsimg
Results 1 to 3 of 3

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2016
    Posts
    9

    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.

    Code:
    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?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,237

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

    A goto might be faster, but a more readable option (that should be at least as fast as goto) is simply put the word "else" in front of the second If statement.

    https://msdn.microsoft.com/en-gb/library/5011f09h.aspx

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,502

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

    I can't even believe that goto is even in the C# lexicon ... that aside, at the machine level, there's not much of a difference between stuffing a goto there and using an elseif ... it's JuMP command either way, so there shouldn't be any efficiency in either method. However, goto is considered archaic, and a poor programming practice, as it can lead to confusion - what if someone stuffs in new code before FirstPointCalculated, then wonders why it doesn't run?

    I'd strongly urge you to avoid future floggings by using the if...elseif construct instead of using a GoTo.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width