Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 536
Vince Avatar asked Apr 09 '14 01:04

Vince


People also ask

What is a good variable name in Python?

A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character.

Does variable name length affect performance Python?

To wrap it up, variable name length can affect a Python program's speed as larger files require longer processing times, as shown in the Unix time benchmark.

What type of variable is a name in Python?

Rules for creating variables in Python A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). Variable names are case-sensitive (name, Name and NAME are three different variables).

Do shorter variable names make code faster?

Computers don't know anything about variable names; they only want to know where values are stored. Variables are symbols, nothing more. They replace hex values with names so that programmers have an easier time understanding what they're doing. So, there will be no performance boost by choosing shorter variable names.


1 Answers

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.

like image 79
Sheng Avatar answered Sep 28 '22 23:09

Sheng