[Python] Is there any performance difference between calling a local variable and calling an...

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    Here's the code I have:

    elif line.mesg.startswith('^^'):
    recog, score = pull_recog(line)
    line.recog = recog
    line.score = score

    print recog + ' '*(20-len(recog)) + '%2.2f'%score


    This question really comes down to how the compiler takes care of this:

    Does the compiler optimize this code in some way that would make no difference between:

    1. Calling the local variables recog and score
    2. Calling the object's instance variables line.score and line.recog?

    My initial instinct that using the local variables would be quicker, but I'm not getting good results from timeit so I can't really tell for sure.

    This is within a for-loop, so optimization does actually matter in this case.

    Continue reading...

Compartilhe esta Página