Fastest way to calculate a heavy function between elements of two different lists

mat

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
Michael Szczesny

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)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fastest way of finding common elements between two list of lists in python

The fastest and easiest way to intersect all lists from two different objects

Fastest way to find matching index between two lists in python?

What is the fastest way to calculate a rolling function with a two dimensional window?

Fastest way to populate a matrix with a function on pairs of elements in two numpy vectors?

What is the fastest way to calculate the logical_and (&&) between elements of two __m256i variables, looking for any pair of non-zero elements

Simple way to find if two different lists contain exactly the same elements?

Fastest way to compare common items in two lists

vba fastest way compare two lists of numbers

Fastest way to calculate 'percentage matched' of two trapezoids

Is it possible to use the elements of two different lists as function parameters in Python?

What is the fastest way to calculate sum of absolute differences between two images in Python?

What is the fastest way of calculate cosine similarity between rows of two same shape matrices

Comparing elements of two different lists

Two common elements between lists

Fastest way to get all unique numbers between two different dimension numpy arrays

Compare elements of two lists and calculate median value

Combine two lists in a way that only those elements of first are replaced with elements of second which are different from each other

Find the elements between matching elements in two lists

Fastest way to find dataframe indexes of column elements that exist as lists

Fastest way to subset sub-elements of lists in R

What is the fastest way to calculate the weighted sum of matrix elements?

Efficient way to calculate all IoUs of two lists

fastest method to get indices of first and last common elements between lists

swapping two elements between two lists in python

The fastest way to check if two lists contain the same object

Fastest way to sum two lists of items with each other in python

C# / LINQ fastest way of comparing two lists and assigning value

Fastest way to calculate in Pandas?