user5662485
user5662485

Reputation:

Performance difference: initialize and override in an if-else block, or an extra "else"?

I'm curious as to which of the following would be more efficient: initializing a variable with a default value and only overriding it if needed in an if-else block, or initializing the variable without a value at all and setting a value in that if-else block?

Here is an example of the former and latter:

Former:

    String weightStatus = "Underweight";

    if (bMI > 29.9)
    {
    weightStatus = "Obese";
    }

    else if (bMI >= 25.0)
    {
    weightStatus = "Overweight";
    }
    
    else if (bMI >= 18.5)
    {
    weightStatus = "Healthy Weight";
    }

Latter:

    String weightStatus;

    if (bMI > 29.9)
    {
    weightStatus = "Obese";
    }
    
    else if (bMI >= 25.0)
    {
    weightStatus = "Overweight";
    }
    
    else if (bMI >= 18.5)
    {
    weightStatus = "Healthy Weight";
    }
    else
    {
    weightStatus = "Underweight";
    }
    

The difference would be small, but I can't help but wonder which one is technically faster based on how variable assignment works.

Upvotes: 0

Views: 58

Answers (1)

Ahmed Ashour
Ahmed Ashour

Reputation: 5549

As you know, the former case, the bytecode will always set the variable, then according to the if/then, it may reset it again.

But it also depends on which values would be passed at runtime, if mostly they are not in then branches, then I guess it won't make much difference, but if they frequently go into then branches, then there is mostly double setting executions.

You can test it with something like:

public class MyTest {

    private static double START = 0;
    private static double END = 100;
    private static double INCREMENT = 0.0001;

    @Test
    public void testFirst() throws Exception {
        long time = System.nanoTime();
        for (double bMI = START; bMI < END; bMI += INCREMENT) {
            first(bMI);
        }
        System.out.println("First  " + (System.nanoTime() - time));
    }

    @Test
    public void testSecond() throws Exception {
        long time = System.nanoTime();
        for (double bMI = START; bMI < END; bMI += INCREMENT) {
            second(bMI);
        }
        System.out.println("Second " + (System.nanoTime() - time));
    }

    private String first(double bMI) {
        String weightStatus = "Underweight";
        if (bMI > 29.9) {
            weightStatus = "Obese";
        } else if (bMI >= 25.0) {
            weightStatus = "Overweight";
        } else if (bMI >= 18.5) {
            weightStatus = "Healthy Weight";
        }
        return weightStatus;
    }

    private String second(double bMI) {
        String weightStatus;
        if (bMI > 29.9) {
            weightStatus = "Obese";
        } else if (bMI >= 25.0) {
            weightStatus = "Overweight";
        } else if (bMI >= 18.5) {
            weightStatus = "Healthy Weight";
        } else {
            weightStatus = "Underweight";
        }
        return weightStatus;
    }
}

Upvotes: 1

Related Questions