pingu2k4
pingu2k4

Reputation: 1050

When returning from an if, is there a perfmance difference on using else if or just an if following it?

Not a problem I'm having, just a thought I had and had no idea.

Assume you want to check through numerous possibilities, for example:

if(x > 1000)
{
    return 1000;
}
if(x > 990)
{
    return 1010;
}
if(x > 980)
{
    return 33;
}
if(x > 970)
{
    return 2;
}
if(x > 960)
{
    return null;
}

In that situation, I could also use else if's instead of just entering a new if, and it would have the exact same effect. Is there a performance difference at all? If there is, what is the "more correct" way to do it?

Upvotes: 1

Views: 43

Answers (3)

Tyler Benzing
Tyler Benzing

Reputation: 1136

I ran some test code using the Stopwatch class and it appears that else ifs are slightly faster than multiple ifs. The difference is only a matter of a few ticks

private void button1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
        sw.Start();
        Bar(1337);
        sw.Stop();

        Console.WriteLine("Else ifs: " + sw.ElapsedTicks);

        sw.Reset();
        sw.Start();
        Foo(1337);
        sw.Stop();

        Console.WriteLine("Multiple Ifs: " + sw.ElapsedTicks);
}

private bool Foo(int x)
    {
        if (x > 1000000)
            return true;

        if (x > 100000)
            return true;

        if (x > 10000)
            return true;

        if (x > 1000)
            return true;

        if (x > 100)
            return true;

        if (x > 10)
            return true;

        if (x > 1)
            return true;

        return false;
    }

    private bool Bar(int x)
    {
        if (x > 1000000)
            return true;
        else if (x > 100000)
            return true;
        else if (x > 10000)
            return true;
        else if (x > 1000)
            return true;
        else if (x > 100)
            return true;
        else if (x > 10)
            return true;
        else if (x > 1)
            return true;
        else
            return false;
    }

Note: C#.NET Code example

Upvotes: 0

Daniel Hall
Daniel Hall

Reputation: 13679

Regarding performance, this could conceivably vary based on compiler, but in general there should be no difference. See answer here:

Performance difference of "if if" vs "if else if"

In terms of which is more correct, that is entirely dependent on intent and logic involved. In your example above, it's perfectly correct to just use "if" (no "else") because each condition early exits, and there is no possibility of ambiguity. However, if your conditions did not early exit, then a single value of x could match multiple conditions. So correctness is based on whether the logic in each condition should be compounded so that for an x with a certain value, all the logic in each matching condition should be applied. Or, whether only one set of logic should run in response to the first condition met (in that case, you would have to use "else if"). I have never encountered any other reason to choose between "if" and "else if" other than intent and whether each condition is distinct and non-overlapping, or cumulative in effect.

Upvotes: 1

Usually no. In normal cases if do not return in each statement if/else if/else is better because in this construction if you find one true condition, you can stop looking at the remaing if blocks.

But in your case, there is not difference.

Upvotes: 3

Related Questions