all() is slow?

Chris Angelico rosuav at gmail.com
Mon Nov 7 17:06:56 EST 2011


On Tue, Nov 8, 2011 at 8:46 AM, david vierra <codewarrior0 at gmail.com> wrote:
> But, you didn't write an all() function.  You wrote a more specialized
> allBoolean() function. I think this comparison is more fair to the
> builtin all():

So really, it's not "all() is slow" but "function calls are slow".
Maybe it'd be worthwhile making an all-factory:

def my_all(code,lst):
    exec("""def tmp_all(x):
        for a in x:
            if not ("""+code+"""): return False
        return True
""")
    return tmp_all(lst)
timeit.timeit('my_all("a in (True, False)",x)','from __main__ import
my_all,x',number=10)

Bad code imho, but it _is_ faster than both the original and the builtin.

ChrisA



More information about the Python-list mailing list