[clean-list] Matrix-Matrix-Multiplication and Cleans memory

Ronny Wichers Schreur ronny@cs.kun.nl
Mon, 06 Nov 2000 09:09:10 +0100


Witold wrote (to the Clean Mailing List):

>[...] array comprehension itself can both slow down execution and
>increase memory usage.
>
>[...]
>
>imageToString :: ![(Char, Char, Char)] -> !{#Char} // !DEF
>imageToString im = { k c \\ (c, k) <- [ (x, y) \\ x <- im, y <- [red,
>         green, blue] ] }
>        where red (r, _, _) = r; green (_, g, _) = g; blue (_, _, b) = b
>
>[uses much more memory than]
>
>imageToString_ :: ![(Char, Char, Char)] -> !{#Char} // !DEF
>imageToString_ im = { emptya & [i] = k c \\
>         i <- [1..(XSIZE * YSIZE * 3)] &    // RWS: i is one to high here
>        (c, k) <- [ (x, y) \\ x <- im, y <- [red, green, blue] ] }
>        where red (r, _, _) = r; green (_, g, _) = g; blue (_, _, b) = b
>
>[...]
>I guess it is because during array comprehension (when the final size
>is unknown) the array is copied many times (as it grows). 

No, the difference in that in the first function the list
generated by [ (x, y) \\ ... ] will be traversed twice. First
to compute its length and then to fill the array. This means
that after the first traversal the whole list is kept in memory.



Cheers,

Ronny Wichers Schreur