[New-bugs-announce] [issue32180] bool() vs len() > 0 on lists

Дилян Палаузов report at bugs.python.org
Thu Nov 30 06:31:47 EST 2017


New submission from Дилян Палаузов <dilyan.palauzov at aegee.org>:

Please make bool() on lists at least as fast as len() > 0 on lists is.

The trivial approach would be to define __bool__ on lists, that do something like  "True if self else False".

python3 
Python 3.6.3+ (heads/3.6-dirty:2b5cbbb13c, Nov  1 2017, 19:03:09) 
[GCC 6.4.1 20171025] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.timeit('len([]) > 0')
0.0983404889702797
>>> timeit.timeit('bool([])')
0.15502946823835373
>>> timeit.timeit('True if [] else False')
0.03108721226453781
>>> timeit.timeit('len([1]) > 0')
0.11656427383422852
>>> timeit.timeit('bool([1])')
0.19317257404327393
>>> timeit.timeit('True if [1] else False')
0.057590410113334656

----------
messages: 307294
nosy: dilyan.palauzov
priority: normal
severity: normal
status: open
title: bool() vs len() > 0 on lists
type: performance
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32180>
_______________________________________


More information about the New-bugs-announce mailing list