Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does adding local variables to methods make them slower?

This question has received a total of several paragraphs of answer. Here is the only sentence that actually tells me what I was looking for:

Your examples would make little difference since intermediate computations need to be stored temporarily on the stack so they can be used later on.

In fact, it answers my question perfectly and completely =)

Unlike all the cruft telling me "nooo don't ask that question". >_<


Like if you have a method, and you change it by increasing the number of local variables but make no other changes, does it make the method slower? Here's an example:

void makeWindow() {
    Display
        .getContext()
        .windowBuilder()
        .setSize(800, 600)
        .setBalloonAnimal(BalloonAnimal.ELDER_GOD.withColor(PUCE))
        .build();
}

or

void makeWindow() {
    DisplayContext dc = Display.getContext();
    WindowBuilder wb = db.windowBuilder();
    BalloonAnimal god = BalloonAnimal.ELDER_GOD;
    BalloonAnimal puceGod = god.withColor(PUCE);
    wb.setSize(800, 600).setBalloonAnimal(puceGod).build();
}

Another example:

int beAnExample(int quiche) {
    return areGlobalsEvil?
        quiche * TAU/5:
        highway(quiche, Globals.frenchFrenchRevolution);
}

or

int beAnExample(int quiche) {
    if (areGlobalsEvil) {
        int to5 = TAU/5;
        int result = quiche * to5;
        return result;
    } else {
        Game french = Globals.frenchFrenchRevolution;
        int result = highway(quiche, french);
        return result;
    }
}

Really, what I'm asking is: Is the number of this sort of local variable even relevant by the time the method's compiled to bytecode? If so, what about once Hotspot gets to work on it?

This question is relevant to the code generator I'm working on.

like image 234
amara Avatar asked Apr 23 '11 04:04

amara


People also ask

Are local variables faster?

The thing about local variables is that the compiler optimizes them to be allocated from the registers if possible, or from the cache if not. This is why local variables are faster.

Does python compute local variables quicker?

Locals should be faster When a line of code asks for the value of a variable x, Python will search for that variable in all the available namespaces, in order: local namespace - specific to the current function or class method.

Why is it better to use local variables than global variables?

So, by using a local variable you decrease the dependencies between your components, i.e. you decrease the complexity of your code. You should only use global variable when you really need to share data, but each variable should always be visible in the smallest scope possible.

What is better local or global variable?

It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.


1 Answers

The easy answer is no. Local variables consume runtime stack space. Allocating space for them only marginally increases the number of instructions. Your examples would make little difference since intermediate computations need to be stored temporarily on the stack so they can be used later on. Focus more on the readability of your programs rather than needless micro-optimizations.

If you're interested in looking at the actual bytecode of a class, investigate the javap program.

like image 175
Edawg Avatar answered Sep 28 '22 06:09

Edawg