why I need so much memory????
Hi, I am trying to make a program to generate all the poker hands. I have done it yet and it only need a few of seconds. Each card is represented with a Char (to make the structure smaller) and then all the hands are a [[Char]] in total the structure has a size of arround 20Mb. Now I like be sure that I have all the hands and that they are not repeated. To do that I have done the next function: isRepeated :: Eq a => [a] -> Bool isRepeated [] = False isRepeated (p:r) = (any (\x -> x == p) r) || isRepeated r I call it with a [[Char]] of 2598960 elements and each element with 5 Characters. Ok the complexity is high but.... why this program needs 1GB (the structure only has 20MB) of memory?? The problem is that it begins to use the swap zone and due to that It can only use the 10% of the processor. I have compiled with the latest version of GHC (ghc --make -C ppal.hs). I attach you the source files (the backtr works well) and the map memory of the process. Alberto Fuentes _________________________________________________________________ Un amor, una aventura, compañía para un viaje. Regístrate gratis en MSN Amor & Amistad. http://match.msn.es/match/mt.cfm?pg=channel&tcid=162349
Alberto Fuentes Rodriguez wrote:
isRepeated :: Eq a => [a] -> Bool isRepeated [] = False isRepeated (p:r) = (any (\x -> x == p) r) || isRepeated r
Two comments: You can write "any (==p) r", using a partial application of the equality predicate. Reads better IMHO. This is O(n²), sorting and checking should be O(n log n). You might be better off generating only unque hands from the start? If you can generate a lazy stream of hands, and the stream can be consumed lazily, it will save a lot or memory. (Three comments, I mean)
I call it with a [[Char]] of 2598960 elements and each element with 5 Characters. Ok the complexity is high but.... why this program needs 1GB (the structure only has 20MB) of memory?? The problem is that it begins to use the swap zone and due to that It can only use the 10% of the processor.
You can try to limit the heap -- for me, about 80% of physical RAM seems to be a good choice. (E.g. +RTS -M800M) -k
Hello,
You can write "any (==p) r", using a partial application of the equality predicate. Reads better IMHO.
OR: you could write the slightly better (see below) version any (p==) r Notice that this is directly partial application, rather than a shorthand for "(flip (==) p)". The compiler probably shouldn't be relied upon to spot that the operator is commutative and substitute the simpler version. Hugs certainly doesn't do this, and the "(p==)" version is noticeably cheaper. I've not tested this on any other implementation. Of course, switching to my version won't reduce the max heap size much! As to whether either version reads better, that's a question to take to the Cafe? I prefer to avoid sections in lectures - there's enough syntax already, and it is good practice in using nameless functions! Paul
On Tue, Nov 08, 2005 at 06:40:36PM +0100, Alberto Fuentes Rodriguez wrote:
Each card is represented with a Char (to make the structure smaller) and then all the hands are a [[Char]] in total the structure has a size of arround 20Mb.
Ok the complexity is high but.... why this program needs 1GB (the structure only has 20MB) of memory??
Perhaps the structers *does* take 1GB, but till now the program didn't require it all in memory at once because of lazy evaluation and GC. Best regards Tomasz
participants (4)
-
Alberto Fuentes Rodriguez -
Ketil Malde -
P.C.Callaghan -
Tomasz Zielonka