python - Creating all combinations of a set and running out of memory -


i need generate every combination of 6 numbers set of 55. believe there 28,989,675 indexes in set of combinations. guess i'm running out of memory, because can generate combinations 4 numbers nothing larger that. how can fix problem?

i'm using modification of code borrowed tutorial here: https://www.youtube.com/watch?v=vyxdqxuiwpu

import itertools  text_file = open("comb3.txt", "w")  harmonics = [28, 33, 36, 38, 40, 43, 45, 47, 48, 50, 52, 55, 55.86, 57, 59, 60, 60.86, 61.69, 62, 63.86, 64, 65.86, 66, 66.69, 67, 69, 69.69, 70.86, 71, 71.69, 72, 74, 75.86, 76, 76.69, 77.86, 79, 81, 81.69, 82.86, 83.69, 84, 84.86, 86, 88, 88.69, 89.86, 90.69, 91, 93, 95, 95.69, 96.86, 98, 100]  combos = itertools.combinations(harmonics, 4)  usable_combos = [] e in combos:     usable_combos.append(e)  print usable_combos  s = str(usable_combos)  text_file.write(s) text_file.close() 

thanks!

one reason why you're running out of memory due fact (as quite rightly stated): 55 choose 6 = 28,989,675

now, think how memory is, exactly. can perform quick back-of-the-envelope calculation estimate how memory take:

since list using floats , integers, can deduce upper bound memory consumption being:

sys.getsizeof(float()) 

which on 64 bit machine 24 bytes, , on 32 bit machine 16 bytes

and, since tuples take up: 56 + 8 * len(t) bytes (64 bit)

hence, upper bound of calculation take:

  1. 28,989,675 * 6 * 24 + 28,989,675 * (56 + 8 * 6) bytes ~ 6,856.39 mib of memory (64 bit)
  2. 28,989,675 * 6 * 16 + 28,989,675 * (56 + 8 * 6) bytes ~ 5,529.34 mib of memory (32 bit)

recalling python lists implemented contiguously (for o(1) lookup time), probable reason why crashes, have consider memory occupied os , other programs in ram.

compare other example cited: 55 choose 4 = 341,055 => ~ 59.85 mib (64 bit) or ~49.44 mib (32 bit) of contiguous memory. since reasonable amount of memory can contiguous, runs without problem.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -