Hey. I have a function I want to memoize, however, it has too many possible values. Is there any convenient way to store the values in a text file and make it read from them? For example, something like storing a pre-computed list of primes up to 10^9 in a text file? I know it’s slow to read from a text file but there’s no other option if the amount of data is really huge. Thanks!
Share
For a list of primes up to
10**9, why do you need a hash? What would the KEYS be?! Sounds like a perfect opportunity for a simple, straightforward binary file! By the Prime Number Theorem, there’s about10**9/ln(10**9)such primes — i.e. 50 millions or a bit less. At 4 bytes per prime, that’s only 200 MB or less — perfect for anarray.array("L")and its methods such asfromfile, etc (see the docs). In many cases you could actually suck all of the 200 MB into memory, but, worst case, you can get a slice of those (e.g. via mmap and thefromstringmethod ofarray.array), do binary searches there (e.g. via bisect), etc, etc.When you DO need a huge key-values store — gigabytes, not a paltry 200 MB!-) — I used to recommend
shelvebut after unpleasant real-life experience with huge shelves (performance, reliability, etc), I currently recommend a database engine instead — sqlite is good and comes with Python, PostgreSQL is even better, non-relational ones such as CouchDB can be better still, and so forth.