Speed of string += string
aleax at aleax.it
Mon Apr 14 09:12:05 CEST 2003
Greg Ewing (using news.cis.dfn.de) wrote:
> achrist at easystreet.com wrote:
>> Now, which is preferred:
>> if astring == "":
>> if len(astring) == 0:
> The first ought to be faster, since it avoids a
> global name lookup and a Python function call,
> both of which are fairly expensive.
> But better still would be simply
> if astring:
...which, however, has exactly the opposite truth value.
Sometimes that will be OK (e.g., whenever you also have
an else clause and switching the two bodies does not
diminish readability), but not always. You can use
"if not astring:" to get the same truth value as
"if len(astring)==0:" more directly. I doubt speed
considerations are very significant at all here, though.
More information about the Python-list