in reply to Avoid keeping larger lists in Memory
I'm running your script and watching the memory usage (top), and it's minimal and also not growing, I don't think that memory is your problem. Instead, I think it's just that the code you've implemented is relatively slow, so you probably want to look into optimizing it. A couple of recommendations there: you could, for example
Alternatively, for the task you're doing I can recommend Math::Prime::Util, it has lots of really helpful mathematical routines and it can deal with huge numbers (you should install Math::Prime::Util::GMP for speed). However, if this is homework, that might be considered cheating ;-)
use Math::Prime::Util qw/factor/; use List::Util qw/max/; print max(factor(20)), "\n"; print max(factor(13195)), "\n"; print max(factor("600851475143")), "\n"; __END__ 5 29 6857
As for your original question, note that once you put things on disk, you'll have disk I/O as a potential bottleneck. But if you still want to look into that, there are a few Perl modules that make storing things on disk fairly transparent, such as AnyDBM_File for tying a hash to a DBM file, or Tie::File for tying an array to a flat text file. Also, this thread might be an interesting read: Is there any cache mechanism for running perl script, where the replies show how to cache data structures using Storable, JSON, or Path::Class (the latter only for simple arrays).
|
|---|