Problem

Consider the following Python function,

def any(boolVec):
    """
    Return True if any of the elements of the input value are True, otherwise, return False.
    """
    result = False
    for element in boolVec:
        result = result or element
    return result

What can you do to the above code to make it more performant?
Prove your suggestion by benchmarking your revised code against the original code above.
For your benchmark, you can generate very long vectors of random Boolean (logical) values to pass them to the function.
For example, you can use the NumPy library’s choice function,

import numpy as np
np.random.choice(a = [False, True], size= 10000)

How much performance improvement do you observe?

Comments