XisUnknown
XisUnknown

Reputation: 125

Does the length of a variable's name affect the speed the program will run?

I have a short bit of code that needs to run for a long long time. I am wondering if the length of the variable's names that I use can alter the speed at which the program executes. Here is a very simple example written in Python.

Program A

    x = 1
    while not x == 0:
          print('message')

Program B

    xyz = 1
    while not xyz == 0:
          print('message')

Will Program A print 'message' more times than Program B if I run program A and program B for 30 years on two identical machines.

Upvotes: 4

Views: 1836

Answers (3)

C-3PO
C-3PO

Reputation: 1213

The results that @chepner mentioned are correct, Python can take longer to run the code in the console, but once the code is compiled the results are the same.

To make sure that this is correct, I created the following code also inspired by the answer from @knifer:

from time import time
from numpy import average,std

x                                              = 1
xyzabcdexyzabcdefghidjakeldkjlkfghidjakeldkjlk = 1

short_runs = 0
long_runs  = 0

for _ in range(int(2e7)):
    
    t0 = time()
    if x:
        pass
    short_runs += time() - t0
    
    t0 = time()
    if xyzabcdexyzabcdefghidjakeldkjlkfghidjakeldkjlk:
        pass
    long_runs  += time() - t0

print('Runtime results:')
print(f"Small variable runs : (sum = {short_runs:.3f})")
print(f"Long  variable runs : (sum = {long_runs :.3f})")

The code I propose is somewhat different, in the sense that the trial runs for the long and the short variable names are intertwined, such that any differences caused by underlying OS processes are minimized.

The results of the code vary depending on whether you copy-paste the code into a Python console, or you call the code as a program (python trial_runs.py). Runs using copy-paste tend to be slower using long variables names, whereas calling the code as a program yields identical running times.

PS. The actual running times change all the time for me (in one direction or the other), so it's hard to report exact values. Even the long variable names can sometimes run faster, although this is very rare on the Python console. The biggest conclusion is that any differences are really small either way :)

Upvotes: 1

knifer
knifer

Reputation: 9

The difference is very small and we cant conclude that is because of name of variable.

import timeit
x=1
xyz=1


start_time = timeit.default_timer()
for i in range(1,1000000):
    if x==1:
        print("message")
elapsed = timeit.default_timer() - start_time


start_time2 = timeit.default_timer()
for i in range(1,1000000):
    if xyz==1:
        print("message")

elapsed2 = timeit.default_timer() - start_time2

print("small variable printing = ",str(elapsed),"big variable printing = "+str(elapsed2))

And the Result was :

small variable printing =  3.6490847053481588 big variable printing = 3.7199463989460435

Upvotes: -1

chepner
chepner

Reputation: 530960

No, the names themselves have no effect on how quickly the resulting code runs. Variable names are just used to distinguish in the Python source two variables that are represented by integer indices into a lookup table:

>>> dis.dis('x=1')
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (x)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE
>>> dis.dis('xyz=1')
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (xyz)
              4 LOAD_CONST               1 (None)
              6 RETURN_VALUE
>>> dis.dis('x=1;xyz=2;')
  1           0 LOAD_CONST               0 (1)
              2 STORE_NAME               0 (x)
              4 LOAD_CONST               1 (2)
              6 STORE_NAME               1 (xyz)
              8 LOAD_CONST               2 (None)
             10 RETURN_VALUE

In the first two, you'll notice no distinction based the variable name is made in the resulting byte code. In the last, you'll see that the byte code differentiates between the two, but only on the order in which they are defined, not the length of the label.

Upvotes: 8

Related Questions