As written in the title I would like to know if there is a faster way than the one I found to perform a computationally complex function between elements of two lists.
Here as an example I will take the factorial but the function is a dynamo function that checks if there are intersections between two elements.
This is what I've been able to do so far. Can anyone help me?
%%time
import random
import itertools
import math
random.seed(10)
list1=[random.randint(1, 100) for i in range(10000)]
list2=[random.randint(1, 10) for i in range(100)]
two_lists=[list1,list2]
permutation = itertools.product(*two_lists) # I obtain the permutation ty Cyttorak
result=[math.factorial(x[0]+x[1]) for x in permutation] # The complex operation (factorial of the sum)
Wall time: 1.11 s
I can only get a 8x speed up with getting rid off the indexing, redundant lists and caching computations. maxsize
is taylored for your example.
from functools import lru_cache
@lru_cache(maxsize=128)
def facto(x):
return math.factorial(x)
result=[facto(x+y) for x,y in itertools.product(list1, list2)]