Do you run this in ghci or in a compiled program? This is of great importance.
If in ghci, ghci will support the calculated test value if you want to use it later. This is usually a good idea, but not in this case, when test is a huge value that would be cheap to recount anyway. How huge? For starters, this is a list of 10 ^ 8 elements and (on a 64-bit system) the list costs 24 bytes per element, so 2.4G is narrower. Then there is the use of the values ββof the values ββof space themselves. You might think that all values ββare taken from [1..100] , so they should be separated and use a small amount of space as a whole. But the values ββin the list really have the form x , which can depend on a , b , c and d , and length never checks the values ββin the list as it goes through. Therefore, each element will be presented as a closure that refers to a , b , c and d , which takes up at least 8 * (4 + 1) = 40 bytes, which brings us to just 6.4G.
This is quite a lot, and the garbage collector should do quite a lot of copying when you allocate 6.4G of data, and all this constantly lives on. This is something that takes so long, and does not compute the list or its length.
If you compile a program
test = [x|a<-[1..100],b<-[1..100],c<-[1..100],d<-[1..100],let x = a] main = print $ length test
then test does not need to be supported live, because its length is calculated, since it is obvious that it will never be used again. So, now GC practically does not work, and the program runs in a couple of seconds (reasonable for ~ 10 ^ 8 lists of node distributions and calculations on Integer ).
source share