Slicing vs .startswith

Terry Reedy tjreedy at udel.edu
Mon Sep 22 19:57:49 EDT 2003


"Shu-Hsien Sheu" <sheu at bu.edu> wrote in message
news:mailman.1064245938.12315.python-list at python.org...
> Hi,
>
> For example, which one of the following would be more efficient, or
,
> moreover, more pythonic?
>
> if aa[:3] == 'abc':

This creates and deletes a temporary object.

> if aa.startswith('abc'):

This makes a function call.  As Bob showed for his system, the two
overheads are about the same for a three char prefix.  If one were
checking a 30000 byte prefix, the call might win.

Terry J. Reedy






More information about the Python-list mailing list