This is a multi-part message in MIME format.

--Boundary_(ID_QJc465QYwg0rfc7kM4YglA)
Content-type: MULTIPART/ALTERNATIVE;
 BOUNDARY="Boundary_(ID_GV3PYkBrWKBkRhXtR3jVMw)"


--Boundary_(ID_GV3PYkBrWKBkRhXtR3jVMw)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8BIT

Juan Jose Garcia Ripoll wrote:

> Hi,
>
> can anybody point me to tutorials, papers, etc, on how to properly
> annotate strictness in Haskell code? I am concerned with the following
> stupid piece of code that eats a lot of memory and takes an incredible
> amount of time to produce some output. I hope somebody will help me in
> finding what I am doing wrong.
>
>         Juanjo
>
> ---
> module Main where
>
> import Array
>
> produce :: Int -> Double -> Array Int Double
> produce n x = array (1,n) [(i,x) | i <- [1..n]]
>
> scprod :: Array Int Double -> Array Int Double -> Double
> scprod a b =
>         case (bounds a, bounds b) of
>           ((1,i), (1,j)) ->
>                 foldl (+) start [a!(x) * b!(x) | x <- [2..i]]
>                 where start = a!(1) * b!(1)
>
> main = print (show (scprod a a))
>        where a = produce 1000000 1.0
>
> --
> Juan Jose Garcia Ripoll         www: http://www.arrakis.es/~worm
> Univ. Castilla-La Mancha        job: [EMAIL PROTECTED]
> ETSI. Industriales              home: [EMAIL PROTECTED]
> Departamento de Matematicas
> c/Camilo Jose Cela, 3, Ciudad Real 13071 Spain


Using GHC and compiling with -O, if you write

scprod a b =sum' [a!x * b!x | x <- [1..snd (bounds a)]]

sum' :: Num a => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs

you'll reduce the amount of memory needed, and also
the execution time, because GHC is able to detect that
sum' is strict.

If you use the predefined function sum instead of sum',
you'll obtain worse results (see attached figures, obtained
using the GHC heap profiler).

Best Regards,
  Fernando

--
 ---------------------------------------------------
  Fernando Rubio Diez
  Dpto. Sistemas Inform�ticos y Programaci�n
  Facultad de Ciencias Matem�ticas
  Universidad Complutense. Madrid
  mailto:[EMAIL PROTECTED]
  http://dalila.sip.ucm.es/miembros/fernando
  phone: (34) 91 394 4350   fax: (34) 91 394 4602
 ---------------------------------------------------



--Boundary_(ID_GV3PYkBrWKBkRhXtR3jVMw)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT

<HTML>
Juan Jose Garcia Ripoll wrote:
<BLOCKQUOTE TYPE=CITE>Hi,

<P>can anybody point me to tutorials, papers, etc, on how to properly
<BR>annotate strictness in Haskell code? I am concerned with the following
<BR>stupid piece of code that eats a lot of memory and takes an incredible
<BR>amount of time to produce some output. I hope somebody will help me
in
<BR>finding what I am doing wrong.

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Juanjo

<P>---
<BR>module Main where

<P>import Array

<P>produce :: Int -> Double -> Array Int Double
<BR>produce n x = array (1,n) [(i,x) | i &lt;- [1..n]]

<P>scprod :: Array Int Double -> Array Int Double -> Double
<BR>scprod a b =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case (bounds a, bounds b)
of
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((1,i), (1,j))
->
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
foldl (+) start [a!(x) * b!(x) | x &lt;- [2..i]]
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
where start = a!(1) * b!(1)

<P>main = print (show (scprod a a))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a = produce 1000000 1.0

<P>--
<BR>Juan Jose Garcia Ripoll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
www: <A HREF="http://www.arrakis.es/~worm">http://www.arrakis.es/~worm</A>
<BR>Univ. Castilla-La Mancha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
job: [EMAIL PROTECTED]
<BR>ETSI. 
Industriales&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
home: [EMAIL PROTECTED]
<BR>Departamento de Matematicas
<BR>c/Camilo Jose Cela, 3, Ciudad Real 13071 Spain</BLOCKQUOTE>
&nbsp;
<BR>Using GHC and compiling with -O, if you write

<P>scprod a b =sum' [a!x * b!x | x &lt;- [1..snd (bounds a)]]

<P>sum' :: Num a => [a] -> a
<BR>sum' [] = 0
<BR>sum' (x:xs) = x + sum' xs

<P>you'll reduce the amount of memory needed, and also
<BR>the execution time, because GHC is able to detect that
<BR>sum' is strict.

<P>If you use the predefined function sum instead of sum',
<BR>you'll obtain worse results (see attached figures, obtained
<BR>using the GHC heap profiler).

<P>Best Regards,
<BR>&nbsp; Fernando
<PRE>--&nbsp;
&nbsp;---------------------------------------------------
&nbsp; Fernando Rubio Diez
&nbsp; Dpto. Sistemas Inform&aacute;ticos y Programaci&oacute;n
&nbsp; Facultad de Ciencias Matem&aacute;ticas
&nbsp; Universidad Complutense. Madrid
&nbsp; <A HREF="mailto:[EMAIL PROTECTED]">mailto:[EMAIL PROTECTED]</A>
&nbsp; <A 
HREF="http://dalila.sip.ucm.es/miembros/fernando">http://dalila.sip.ucm.es/miembros/fernando</A>
&nbsp; phone: (34) 91 394 4350&nbsp;&nbsp; fax: (34) 91 394 4602
&nbsp;---------------------------------------------------</PRE>
&nbsp;</HTML>

--Boundary_(ID_GV3PYkBrWKBkRhXtR3jVMw)--

--Boundary_(ID_QJc465QYwg0rfc7kM4YglA)
Content-type: application/postscript; name=suma.ps
Content-disposition: inline; filename=suma.ps
Content-transfer-encoding: 7bit

%!PS-Adobe-2.0
%%Title: a.out +RTS -hD -i1.00 -RTS
%%Creator: hp2ps (version 0.25)
%%CreationDate: Mon Jun  7 09:57 1999
%%EndComments
-90 rotate
-756.000000 72.000000 translate
/HE10 /Helvetica findfont 10 scalefont def
/HE12 /Helvetica findfont 12 scalefont def
newpath
0 0 moveto
0 432.000000 rlineto
648.000000 0 rlineto
0 -432.000000 rlineto
closepath
0.500000 setlinewidth
stroke
newpath
5.000000 407.000000 moveto
0 20.000000 rlineto
638.000000 0 rlineto
0 -20.000000 rlineto
closepath
0.500000 setlinewidth
stroke
HE12 setfont
11.000000 413.000000 moveto
(a.out +RTS -hD -i1.00 -RTS) show
HE12 setfont
(3,944,206 bytes x seconds)
dup stringwidth pop
2 div
319.000000
exch sub
413.000000 moveto
show
HE12 setfont
(Mon Jun  7 09:57 1999)
dup stringwidth pop
637.000000
exch sub
413.000000 moveto
show
45.000000 20.000000 moveto
453.448806 0 rlineto
0.500000 setlinewidth
stroke
HE10 setfont
(seconds)
dup stringwidth pop
498.448806
exch sub
5.000000 moveto
show
45.000000 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(0.0)
dup stringwidth pop
2 div
45.000000 exch sub
5.000000 moveto
show
123.180829 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(0.5)
dup stringwidth pop
2 div
123.180829 exch sub
5.000000 moveto
show
201.361657 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(1.0)
dup stringwidth pop
2 div
201.361657 exch sub
5.000000 moveto
show
279.542486 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(1.5)
dup stringwidth pop
2 div
279.542486 exch sub
5.000000 moveto
show
357.723314 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(2.0)
dup stringwidth pop
2 div
357.723314 exch sub
5.000000 moveto
show
435.904143 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(2.5)
dup stringwidth pop
2 div
435.904143 exch sub
5.000000 moveto
show
45.000000 20.000000 moveto
0 382.000000 rlineto
0.500000 setlinewidth
stroke
gsave
HE10 setfont
(bytes)
dup stringwidth pop
402.000000
exch sub
40.000000 exch
translate
90 rotate
0 0 moveto
show
grestore
45.000000 20.000000 moveto
-4 0 rlineto
stroke
HE10 setfont
(0k)
dup stringwidth
2 div
20.000000 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 58.115308 moveto
-4 0 rlineto
stroke
HE10 setfont
(200k)
dup stringwidth
2 div
58.115308 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 96.230616 moveto
-4 0 rlineto
stroke
HE10 setfont
(400k)
dup stringwidth
2 div
96.230616 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 134.345923 moveto
-4 0 rlineto
stroke
HE10 setfont
(600k)
dup stringwidth
2 div
134.345923 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 172.461231 moveto
-4 0 rlineto
stroke
HE10 setfont
(800k)
dup stringwidth
2 div
172.461231 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 210.576539 moveto
-4 0 rlineto
stroke
HE10 setfont
(1,000k)
dup stringwidth
2 div
210.576539 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 248.691847 moveto
-4 0 rlineto
stroke
HE10 setfont
(1,200k)
dup stringwidth
2 div
248.691847 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 286.807155 moveto
-4 0 rlineto
stroke
HE10 setfont
(1,400k)
dup stringwidth
2 div
286.807155 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 324.922462 moveto
-4 0 rlineto
stroke
HE10 setfont
(1,600k)
dup stringwidth
2 div
324.922462 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 363.037770 moveto
-4 0 rlineto
stroke
HE10 setfont
(1,800k)
dup stringwidth
2 div
363.037770 exch sub
exch
40.000000 exch sub
exch
moveto
show
503.448806 140.333333 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.000000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 142.333333 moveto
(PTR-ARRAY(mut)) show
503.448806 267.666667 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.200000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 269.666667 moveto
(PTR-ARRAY(immut)) show
45.000000 20.000000 moveto
45.000000 20.000000 lineto
204.488890 20.000000 lineto
367.105014 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
367.105014 20.003049 lineto
204.488890 401.158414 lineto
45.000000 20.000000 lineto
closepath
gsave
0.000000 setgray
fill
grestore
stroke
45.000000 20.000000 moveto
45.000000 20.000000 lineto
204.488890 401.158414 lineto
367.105014 20.003049 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
367.105014 402.000000 lineto
204.488890 401.158414 lineto
45.000000 20.000000 lineto
closepath
gsave
0.200000 setgray
fill
grestore
stroke
showpage

--Boundary_(ID_QJc465QYwg0rfc7kM4YglA)
Content-type: application/postscript; name=sum.ps
Content-disposition: inline; filename=sum.ps
Content-transfer-encoding: 7bit

%!PS-Adobe-2.0
%%Title: a.out +RTS -hD -i1.00 -RTS
%%Creator: hp2ps (version 0.25)
%%CreationDate: Mon Jun  7 10:37 1999
%%EndComments
-90 rotate
-756.000000 72.000000 translate
/HE10 /Helvetica findfont 10 scalefont def
/HE12 /Helvetica findfont 12 scalefont def
newpath
0 0 moveto
0 432.000000 rlineto
648.000000 0 rlineto
0 -432.000000 rlineto
closepath
0.500000 setlinewidth
stroke
newpath
5.000000 407.000000 moveto
0 20.000000 rlineto
638.000000 0 rlineto
0 -20.000000 rlineto
closepath
0.500000 setlinewidth
stroke
HE12 setfont
11.000000 413.000000 moveto
(a.out +RTS -hD -i1.00 -RTS) show
HE12 setfont
(45,843,237 bytes x seconds)
dup stringwidth pop
2 div
319.000000
exch sub
413.000000 moveto
show
HE12 setfont
(Mon Jun  7 10:37 1999)
dup stringwidth pop
637.000000
exch sub
413.000000 moveto
show
45.000000 20.000000 moveto
453.448806 0 rlineto
0.500000 setlinewidth
stroke
HE10 setfont
(seconds)
dup stringwidth pop
498.448806
exch sub
5.000000 moveto
show
45.000000 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(0.0)
dup stringwidth pop
2 div
45.000000 exch sub
5.000000 moveto
show
90.344881 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(0.5)
dup stringwidth pop
2 div
90.344881 exch sub
5.000000 moveto
show
135.689761 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(1.0)
dup stringwidth pop
2 div
135.689761 exch sub
5.000000 moveto
show
181.034642 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(1.5)
dup stringwidth pop
2 div
181.034642 exch sub
5.000000 moveto
show
226.379522 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(2.0)
dup stringwidth pop
2 div
226.379522 exch sub
5.000000 moveto
show
271.724403 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(2.5)
dup stringwidth pop
2 div
271.724403 exch sub
5.000000 moveto
show
317.069284 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(3.0)
dup stringwidth pop
2 div
317.069284 exch sub
5.000000 moveto
show
362.414164 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(3.5)
dup stringwidth pop
2 div
362.414164 exch sub
5.000000 moveto
show
407.759045 20.000000 moveto
0 -4 rlineto
stroke
HE10 setfont
(4.0)
dup stringwidth pop
2 div
407.759045 exch sub
5.000000 moveto
show
45.000000 20.000000 moveto
0 382.000000 rlineto
0.500000 setlinewidth
stroke
gsave
HE10 setfont
(bytes)
dup stringwidth pop
402.000000
exch sub
40.000000 exch
translate
90 rotate
0 0 moveto
show
grestore
45.000000 20.000000 moveto
-4 0 rlineto
stroke
HE10 setfont
(0M)
dup stringwidth
2 div
20.000000 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 55.817647 moveto
-4 0 rlineto
stroke
HE10 setfont
(2M)
dup stringwidth
2 div
55.817647 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 91.635293 moveto
-4 0 rlineto
stroke
HE10 setfont
(4M)
dup stringwidth
2 div
91.635293 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 127.452940 moveto
-4 0 rlineto
stroke
HE10 setfont
(6M)
dup stringwidth
2 div
127.452940 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 163.270586 moveto
-4 0 rlineto
stroke
HE10 setfont
(8M)
dup stringwidth
2 div
163.270586 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 199.088233 moveto
-4 0 rlineto
stroke
HE10 setfont
(10M)
dup stringwidth
2 div
199.088233 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 234.905879 moveto
-4 0 rlineto
stroke
HE10 setfont
(12M)
dup stringwidth
2 div
234.905879 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 270.723526 moveto
-4 0 rlineto
stroke
HE10 setfont
(14M)
dup stringwidth
2 div
270.723526 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 306.541172 moveto
-4 0 rlineto
stroke
HE10 setfont
(16M)
dup stringwidth
2 div
306.541172 exch sub
exch
40.000000 exch sub
exch
moveto
show
45.000000 342.358819 moveto
-4 0 rlineto
stroke
HE10 setfont
(18M)
dup stringwidth
2 div
342.358819 exch sub
exch
40.000000 exch sub
exch
moveto
show
503.448806 76.666667 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.000000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 78.666667 moveto
(PTR-ARRAY(mut)) show
503.448806 140.333333 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.200000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 142.333333 moveto
(PTR-ARRAY(immut)) show
503.448806 204.000000 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.600000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 206.000000 moveto
(I#) show
503.448806 267.666667 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.300000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 269.666667 moveto
(<PrelList.$xv_sfSU>) show
503.448806 331.333333 moveto
0 14 rlineto
14 0 rlineto
0 -14 rlineto
closepath
gsave
0.900000 setgray
fill
grestore
stroke
HE10 setfont
522.448806 333.333333 moveto
(<Main.$xk_s4jb>) show
45.000000 20.000000 moveto
45.000000 20.000000 lineto
139.317352 20.000000 lineto
371.483140 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000287 lineto
371.483140 20.000287 lineto
139.317352 55.818148 lineto
45.000000 20.000000 lineto
closepath
gsave
0.000000 setgray
fill
grestore
stroke
45.000000 20.000000 moveto
45.000000 20.000000 lineto
139.317352 55.818148 lineto
371.483140 20.000287 lineto
498.448806 20.000287 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.079372 lineto
371.483140 55.818148 lineto
139.317352 55.818148 lineto
45.000000 20.000000 lineto
closepath
gsave
0.200000 setgray
fill
grestore
stroke
45.000000 20.000000 moveto
45.000000 20.000000 lineto
139.317352 55.818148 lineto
371.483140 55.818148 lineto
498.448806 20.079372 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.236826 lineto
371.483140 118.760355 lineto
139.317352 55.818148 lineto
45.000000 20.000000 lineto
closepath
gsave
0.600000 setgray
fill
grestore
stroke
45.000000 20.000000 moveto
45.000000 20.000000 lineto
139.317352 55.818148 lineto
371.483140 118.760355 lineto
498.448806 20.236826 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.236826 lineto
371.483140 244.644482 lineto
139.317352 55.818148 lineto
45.000000 20.000000 lineto
closepath
gsave
0.300000 setgray
fill
grestore
stroke
45.000000 20.000000 moveto
45.000000 20.000000 lineto
139.317352 55.818148 lineto
371.483140 244.644482 lineto
498.448806 20.236826 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.000000 lineto
498.448806 20.236826 lineto
371.483140 402.000000 lineto
139.317352 55.818148 lineto
45.000000 20.000000 lineto
closepath
gsave
0.900000 setgray
fill
grestore
stroke
showpage

--Boundary_(ID_QJc465QYwg0rfc7kM4YglA)--


Reply via email to