Home > Blockchain >  make function memory efficent or store data somewhere else to avoid memory error
make function memory efficent or store data somewhere else to avoid memory error

Time:06-11

I currently have a for loop which is finding and storing combinations in a list. The possible combinations are very large and I need to be able to access the combos.

can I use an empty relational db like SQLite to store my list on a disk instead of using list = []?

Essentially what I am asking is whether there is a db equivalent to list = [] that I can use to store the combinations generated via my script?

Edit:

SQLlite is not a must. Any will work if it can accomplish my task.

Here is the exact function that is causing me so much trouble. Maybe there is a better solution in general.

Idea - Could I insert the list into the database on each loop and then empty the list? Basically, create a list on each loop, send that list to PostgreSQL and then empty the list in the python to keep the RAM usage down?

def permute(set1, set2):
    set1_combos = list(combinations(set1, 2))
    set2_combos = list(combinations(set2, 8))
    full_sets = []
    for i in set1_combos:
        for j in set2_combos:
            full_sets.append(i   j)
    return full_sets

CodePudding user response:

Ok, a few ideas

My first thought was, why do you explode the combinations objects in lists? But of course, since we have two nested for loops, the iterator in the inner loop is consumed at the first iteration of the outer loop if it is not converted to a list.

However, you don't need to explode both objects: you can explode just the smaller one. For instance, if both our sets are made of 50 elements, the combinations of 2 elements are 1225 with a memsize (if the items are integers) of about 120 bytes each, i.e. 147KB, while the combinations of 8 elements are 5.36e 08 with a memsize of about 336 bytes, i.e. 180GB. So the first thing is, keep the larger combo set as a combinations object and iterate over it in the outer loop. By the way, this will also be really faster.

Now the database part. I assume a relational DBMS, be it SQLite or anything.

You want to create a table with a single column defined. Each row of your table will contain one final combination. Instead of appending each combination to a list, you will insert it in the table.

Now the question is, how do you need to access the data you created? Do you just need to iterate over the final combos sequentially, or do you need to query them, for instance finding all the combos which contain one specific value?

In the latter case, you'll want to define your column as the Primay Key, so your queries will be efficient; otherwise, you will save space on disk using an auto incrementing integer as the PK (SQLite will create it for you if you don't explicitly define a PK, and so will do a few other DMBS as well).

One final note: the insert phase may be painfully slow if you don't take some specific measures: check this very interesting SO post for details. In short, with a few optimizations they were able to pass from 85 to over 96K insert per second.

CodePudding user response:

I don't believe SQLite has a built in array data type. Other DBMSs, such as PostgreSQL, do.

For SQLite, a good recommendation by another user on this site to obtain an array in SQLite can be found here: How to store array in one column in Sqlite3?

Another solution can be found: https://sqlite.org/forum/info/99a33767e8a07e59

In either case, yes it is possible to have a DBMS like SQLite store an array (list) type. However, it may require a little setup depending on the DBMS.

Edit: If you're having memory issues, have you thought about storing your data as a string and accessing the portions of the string you need when you need it?

  • Related