speeding up string.split()

Alex Martelli aleaxit at yahoo.com
Mon May 28 05:10:57 EDT 2001


"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn9h43ca.o3.scarblac at pino.selwerd.nl...
> jcm <grumble at usa.net> wrote in comp.lang.python:
> > It'd be Nice if the Python parser (or bytecode-compiler or whatever
> > non-runtime thing is most appropriate) would translate the addition of
> > two string literals into a single string.  Or maybe it does this
> > already?  I haven't tried.
>
> Python does *no* magic optimizations. It's completely WYSIWYG in that
> respect. You tell it to add the two strings, Python does that, at runtime
> like every other addition.

OTOH, the JUXTAPOSITION of string literals (with just whitespace
in-between, *NO* + operation) *IS* optimized as jcm requires:

D:\ian\good>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> def f(): return "hello " 'world'
...
>>> f()
'hello world'
>>> import dis
>>> dis.dis(f)
          0 SET_LINENO               1

          3 SET_LINENO               1
          6 LOAD_CONST               1 ('hello world')
          9 RETURN_VALUE
         10 LOAD_CONST               0 (None)
         13 RETURN_VALUE
>>>


Alex






More information about the Python-list mailing list