Python variable name and program performance

When I program, I like to give my variables a very meaningful name. In my opinion, in C ++ and another compiled language, these two lines are completely equivalent:

line1:

bool a = true;

line 2:

bool bob_likes_very_much_to_eat_strawberry_on_friday_evening = true;

the reason is this: it will be compiled, and variable names will be lost in the process (?).

On the other hand, and this is what I ask, it seems that the following two will not be equivalent in python:

line1:

a = True

line 2:

bob_likes_very_much_to_eat_strawberry_on_friday_evening = True

the reason this is interpreted, and the interpreter will use some dictionaries with the variable name as the key that will request (?). I have no idea how these dictionaries are implemented, but it doesn't seem crazy to me that a long hash name can take longer (?), And in some cases it affects.

, , ?

1: : , - ? python ( , , , python )

2: , -, . , , , - .

+4
2

. Python 2.7.6 Win7 64 Bit.

>>> import timeit
>>> timeit.timeit(stmt="a = True", number=1000000000)
33.17448742396358
>>> timeit.timeit(stmt="bob_likes_very_much_to_eat_strawberry_on_friday_evening = True", number=1000000000)
32.47728300208675
>>> timeit.timeit(stmt="bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening_bob_likes_very_much_to_eat_strawberry_on_friday_evening = True", number=1000000000)
33.11944278143642

, .

, , .

, varialbe . , . .

+3
import timeit

a = timeit.Timer(stmt="a = 0")
a.repeat()

# => [0.025734134655067464, 0.025691468425065977, 0.025745867864316097]

b = timeit.Timer(stmt="bob_likes_very_much_to_eat_strawberry_on_friday_evening = 0")
b.repeat()
# => [0.025780711948755197, 0.025762934357771883, 0.02595848789496813]

... 0.2% .

30 .

+1

Source: https://habr.com/ru/post/1535664/


All Articles