Vince
Vince

Reputation: 4401

Name of variable in python and program efficiency

When I program, I like to give my variables very meaningful name. From my understanding, in C++ and other compiled language, these two lines are totally equivalent:

line1:

bool a = true;

line 2:

bool bob_likes_very_much_to_eat_strawberry_on_friday_evening = true;

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

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

line1:

a = True

line 2:

bob_likes_very_much_to_eat_strawberry_on_friday_evening = True

the reason being it is interpreted, and the interpreter will use some dictionaries with variable name as key that it will be querying (?). I have no idea how these dictionaries are implemented, but that does not sound crazy to me that hashing long variable name might takes longer (?), and in certain cases have an impact.

So, should I be careful in certain case to keep my variable name length reasonable ?

note 1: this thread is very similar: Does how you name a variable have any impact on the memory usage of an application? but no clear answer concerning python emerges (selected answer says it has an impact on interpreted languages in general, other answers say it does not have impact on python in particular)

note 2: my point is not to advocate misplaced micro-optimizations that result of unreadable code. I want to be aware if by giving extra long names, I am doing some sort of compromise on execution speed or not.

Upvotes: 7

Views: 2070

Answers (2)

Sheng
Sheng

Reputation: 3555

The difference should be very small. But I got some different result from the first answer on Python 2.7.6 with 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

So, it should be implement and platform depending.

In terms of memory usage, in consideration of page alignment, the longer variable name should take the same of more space.

In addition, even if the long varialbe name cost a little more time and space. I will definitely use it if it is more meaningful and easy to understand. That is much more important than efficient.

Upvotes: 3

Hugh Bothwell
Hugh Bothwell

Reputation: 56634

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]

... the long variable name appears to be about 0.2% slower.

It will also use an extra 30-or-so bytes of memory.

Upvotes: 1

Related Questions