Marquisk2
Marquisk2

Reputation: 159

Python Itertools.product speed up

AI have multiple arrays I'm putting into the itertools.product().

Array lengths:

A - 108
B - 126
C - 5
D - 8
E  -8

It takes me a good few minutes to process the arrays and I was wondering if there's another way to speed it up.

total = 0
maxPoints = 0

for combination in itertools.product(A, B, C, D, E):
currentTotal  = int(combination[0][1]) + int(combination[1][0][1]) + int(combination[1][1][1]) + int(combination[1][2][1]) +int(combination[2][0][1]) + int(combination[2][1][1])  + int(combination[2][2][1])  + int(combination[1][0][1])  + int(combination[1][1][1])
if currentTotal < 50000:
    pointTotal  = float(combination[0][2]) + float(combination[1][0][2]) + float(combination[1][1][2]) + float(combination[1][2][2]) + float(combination[2][0][2]) + float(combination[2][1][2])  + float(combination[2][2][2]) +  float(combination[1][0][2]) + float(combination[1][1][2])
    if currentTotal >= total:
        if pointTotal > maxPoints:
            total = currentTotal
            maxPoints = currentPoints

Upvotes: 1

Views: 2047

Answers (1)

Ciaran Liedeman
Ciaran Liedeman

Reputation: 779

You probably won't be able to optimize that any further using just pure python code.

For any serious number crunching I would suggest using numpy.

Here is a cartersian product implementation in numpy that you can use:

Upvotes: 2

Related Questions