Python Forum

Full Version: Need tips for speeding up python code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
from numpy import array, bitwise_or
from itertools import combinations, chain 

n,k=map(int,input().split())
arr=list(map(int,input().split()))
tup=map(list,chain.from_iterable(map(lambda x:combinations(arr,x),range(1,k+1)))) 
ans=map(lambda x:bitwise_or.reduce(array(x,dtype='int')),tup)
print(len(set(ans)))
n is the number of elements in set
the code makes subsets of size <= k and performs bitwise or on each subset and prints the number of unique elements formed
Do you know what part is slow? If not, you might want to try profiling it.