Reputation:
I am writing a code in python in which I need all possible combinations of up to 1000 elements and then perform multiple actions on them. Is there any way to reduce the run time. By using itertools the run time is in minutes after just 50 elements. Currently I am using this code (I have commented out the print statement because it significantly increases the run time):
import itertools
import time
def all_combos_func(arguments):
data = list(arguments)
return itertools.chain.from_iterable(itertools.combinations(data, r) for r in range(len(data)+1))
start_time = time.time()
all_comb= []
for i in range (1000):
all_comb.append(i+1)
all_combos = all_combos_func(all_comb)
all_combos_count = []
for i in all_combos:
all_combos_count.append(i)
# print(i)
print("this is the total length", len(all_combos_count))
end_time = time.time()
print(end_time-start_time)
Upvotes: 0
Views: 412
Reputation: 4439
>>> len(all_combos_func(range(0))
1
>>> len(all_combos_func(range(1))
2
>>> len(all_combos_func(range(2))
4
>>> len(all_combos_func(range(3))
8
>>> len(all_combos_func(range(4))
16
Your all_combos_func
is returning 2^(len(input))
combinations. So when you ask for all combinations and you pass in a 1000 element list, you get back 2^1000 results. Yeah, it's gonna take a while.
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
Upvotes: 1