Is empty string cached?

Steve Holden steve at holdenweb.com
Wed Feb 15 23:15:14 EST 2006


Farshid Lashkari wrote:
>>I really don't understand why it's so important: it's not a part of the 
>>language definition at all, and therefore whatever behavior you see is 
>>simply an artifact of the implementation you observe.
> 
> 
> I guess I should rephrase my question in the form of an example. Should 
> I assume that a new string object is created in each iteration of the 
> following loop?
> 
> for x in xrange(1000000):
>      func(x,'some string')
> 
> Or would it be better to do the following?
> 
> stringVal = 'some string'
> for x in xrange(1000000):
>      func(x,stringVal)
> 
> Or, like you stated, is it not important at all?
> 
It doesn't make a lot of difference:

  >>> import dis
  >>> print dis.dis.__doc__
Disassemble classes, methods, functions, or code.

     With no argument, disassemble the last traceback.


  >>> help(dis.dis)

  >>> dis.dis(compile("""\
  ... for x in xrange(1000000):
  ...      func(x,'some string')""", "", 'exec'))
   1           0 SETUP_LOOP              33 (to 36)
               3 LOAD_NAME                0 (xrange)
               6 LOAD_CONST               0 (1000000)
               9 CALL_FUNCTION            1
              12 GET_ITER
         >>   13 FOR_ITER                19 (to 35)
              16 STORE_NAME               1 (x)

   2          19 LOAD_NAME                2 (func)
              22 LOAD_NAME                1 (x)
              25 LOAD_CONST               1 ('some string')
              28 CALL_FUNCTION            2
              31 POP_TOP
              32 JUMP_ABSOLUTE           13
         >>   35 POP_BLOCK
         >>   36 LOAD_CONST               2 (None)
              39 RETURN_VALUE
  >>> dis.dis(compile("""\
  ... stringVal = 'some string'
  ... for x in xrange(1000000):
  ...      func(x,stringVal)""", "", 'exec'))
   1           0 LOAD_CONST               0 ('some string')
               3 STORE_NAME               0 (stringVal)

   2           6 SETUP_LOOP              33 (to 42)
               9 LOAD_NAME                1 (xrange)
              12 LOAD_CONST               1 (1000000)
              15 CALL_FUNCTION            1
              18 GET_ITER
         >>   19 FOR_ITER                19 (to 41)
              22 STORE_NAME               2 (x)

   3          25 LOAD_NAME                3 (func)
              28 LOAD_NAME                2 (x)
              31 LOAD_NAME                0 (stringVal)
              34 CALL_FUNCTION            2
              37 POP_TOP
              38 JUMP_ABSOLUTE           19
         >>   41 POP_BLOCK
         >>   42 LOAD_CONST               2 (None)
              45 RETURN_VALUE
  >>>

It just boils down to either a LOAD_CONST vs. a LOAD_NAME - either way 
the string isn't duplicated.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list