Search code examples
pythonperformancelistexponentiation

Python exponentiation of two lists performance


Is there a way to compute the Cobb-Douglas utility function faster in Python. I run it millions of time, so a speed increase would help. The function raises elements of quantities_list to power of corresponding elements of exponents list, and then multiplies all the resulting elements.

n = 10

quantities = range(n)
exponents = range(n)

def Cobb_Douglas(quantities_list, exponents_list):
    number_of_variables = len(quantities_list)
    value = 1
    for variable in xrange(number_of_variables):
        value *= quantities_list[variable] ** exponents_list[variable]
    return value

t0 = time.time()
for i in xrange(100000):
    Cobb_Douglas(quantities, exponents)
t1 = time.time()
print t1-t0

Solution

  • Iterators are your friend. I got a 28% speedup on my computer by switching your loop to this:

    for q, e in itertools.izip(quantities_list, exponents_list):
        value *= q ** e
    

    I also got similar results when switching your loop to a functools.reduce call, so it's not worth providing a code sample.


    In general, numpy is the right choice for fast arithmetic operations, but numpy's largest integer type is 64 bits, which won't hold the result for your example. If you're using a different numeric range or arithmetic type, numpy is king:

    quantities = np.array(quantities, dtype=np.int64)
    exponents = np.array(exponents, dtype=np.int64)
    
    def Cobb_Douglas(quantities_list, exponents_list):
        return np.product(np.power(quantities_list, exponents_list))
    # result: 2649120435010011136
    # actual: 21577941222941856209168026828800000