Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Network client - reading and writing to a    socket
      (Manfred Lotz)
   2.  force strict expression evaluation (Ovidiu Deac)
   3. Re:  force strict expression evaluation (Felipe Almeida Lessa)
   4. Re:  force strict expression evaluation (Daniel Fischer)
   5. Re:  force strict expression evaluation (Ovidiu Deac)
   6.  parallel quicksort (Ovidiu Deac)
   7. Re:  parallel quicksort (Ovidiu Deac)


----------------------------------------------------------------------

Message: 1
Date: Tue, 2 Aug 2011 13:39:32 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] Network client - reading and writing
        to a    socket
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, 2 Aug 2011 02:59:44 +0200
Chadda? Fouch? <[email protected]> wrote:

> On Mon, Aug 1, 2011 at 9:16 PM, Manfred Lotz <[email protected]>
> wrote:
> > On Sun, 31 Jul 2011 15:14:23 -0400
> > Patrick LeBoutillier <[email protected]> wrote:
> >
> >> Manfred,
> >>
> >> > The problem is that the message itself is some 30K big and I only
> >> > get some 16K of the message.
> >> >
> >> > How could I force to get the whole message?
> >>
> >> My guess is that you can't. This call:
> >>
> >> c' <- B.hGetNonBlocking h 40000
> >>
> >> tries to read as much as it can (up to 40000 bytes) but it won't
> >> block to wait for data. Perhaps the rest of your message is in a
> >> different TCP packet or delayed or whatever, but I think you have
> >> to keep on reading (and maybe block) until you know you have read
> >> the entire message. The IMAP specs will tell you how to identify
> >> the "end of the message".
> >>
> >> BTW: This issue is not Haskell specific. If you implement the same
> >> code in C, Perl or Java you will have to deal with the same
> >> problem. When you read from a socket, there is no general way of
> >> knowing that the other side has sent everything.
> >>
> >
> > Hmm. I'm not quite sure you are fully right. On the one hand I
> > believe that this could be an issue which arises in python/perl
> > etc. as well. On the other hand I believe it should be possible to
> > receive from a socket what is available at a certain point of time.
> 
> Yes and that's exactly what you have done : your hGetLine blocks until
> there is something to read on the socket and then your hGetNonBlocking
> gets *everything* there is to read on this socket at this exact
> moment... Except as had been said by others that your message has been
> split into several packets and they're not all there when you hGet.
> 
> What you want is not "what is available at a certain point of time",
> you want to read a whole message, except that this "message" notion is
> only in your head (and in a specific protocol, IMAP here) it has no
> direct relevance to how things happens on the network.
> 
> > I found this link http://sequence.complete.org/node/257, and when I
> > run the code I get the full message from the imap server even if the
> > message is a couple of megabytes big.
> 
> Of course you get the whole message ! This code try to read (with
> blocking calls) forever what's on your socket, it reads it, send it on
> a TChan and then retry reading it, bit by bit it gets your whole
> message, of course it has no idea that it got your whole message and
> if nothing is done it will continue to wait on your socket for all
> eternity...
> The key point is this function :
> 
> > listenLoop :: IO a -> TChan a -> IO ()
> > listenLoop act chan =
> >       sequence_ (repeat (act >>= atomically . writeTChan chan))
> 
> This does not stop short of an exception. Two of those loops are
> started each in their own thread (so that they don't block the rest of
> the program) to read stdin and a socket respectively.
> 
> > I have to figure out how to use the code for my need as I do not get
> > the input from the keyboard.
> 
> This code is probably not doing what you want, this is a toy example
> where most of the complexity comes from handling two source of input
> simultaneously and collating their answer on the same TChan. Its main

The code works pretty well, and nevertheless you are right. Because it
is operating in its own thread it doesn't matter if it blocks. 


I guess I understand now. I have to know what to expect back from a
certain command (so that I can adjust the receive accordingly) I did
send to the imap server. 

Thanks to you and the others for explaining.


-- 
Manfred





------------------------------

Message: 2
Date: Tue, 2 Aug 2011 18:58:59 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] force strict expression evaluation
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7sVK7jxFvt6iAhGnURdHShFU86gCy=0D=ijZE=fdk+...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I'm trying to do some performance evaluation and I'm stuck with the
fact that Haskell's lazy evaluation - which make my sort extremely
fast :)

I read this page:
http://www.haskell.org/haskellwiki/Performance/Strictness but I didn't
get it so I'm asking here: How do I make the function 'measure' to
actually force the evaluation of (f p)?

Thanks,
ovidiu

See the code below:
------------------------------------
module Main where
import Prelude
import Data.List
import Data.Time.Clock
import System.Random

quickSort [] = []
quickSort (x:xs) = (quickSort small) ++ [x] ++ (quickSort big)
        where
            small = [p | p <- xs, p <= x]
            big = [p | p <- xs, p > x]

randomlist :: Int -> StdGen -> [Int]
randomlist n = take n . unfoldr (Just . random)

len = 10 ^ 10

measure f p = do
    t1 <- getCurrentTime
    let sorted = f p
    t2 <- getCurrentTime
    let diff = diffUTCTime t2 t1
    return diff

main = do
    seed  <- newStdGen
    let rs = randomlist len seed

    putStrLn $ "Sorting " ++ (show len) ++ " elements..."

    t <- measure quickSort rs

    putStrLn $ "Time elapsed: " ++ (show t)



------------------------------

Message: 3
Date: Tue, 2 Aug 2011 13:11:15 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] force strict expression evaluation
To: Ovidiu Deac <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <CANd=OGGG5r97JY2iRb2ibXw4QpNGY9=DdFgKnmv=9xpgdqq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Tue, Aug 2, 2011 at 12:58 PM, Ovidiu Deac <[email protected]> wrote:
> I'm trying to do some performance evaluation and I'm stuck with the
> fact that Haskell's lazy evaluation - which make my sort extremely
> fast :)

You'll probably want to take a look at criterion [1].

[1] http://hackage.haskell.org/package/criterion

> I read this page:
> http://www.haskell.org/haskellwiki/Performance/Strictness but I didn't
> get it so I'm asking here: How do I make the function 'measure' to
> actually force the evaluation of (f p)?

One possible solution is:

  import Control.Exception (evaluate)
  import Control.DeepSeq (rnf)

  ... = do
    ...
    evaluate (rnf sorted)
    ...

HTH,

-- 
Felipe.



------------------------------

Message: 4
Date: Tue, 2 Aug 2011 18:16:58 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] force strict expression evaluation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Tuesday 02 August 2011, 17:58:59, Ovidiu Deac wrote:
> I'm trying to do some performance evaluation and I'm stuck with the
> fact that Haskell's lazy evaluation - which make my sort extremely
> fast :)

Well, *I* can do nothing even faster ;)

> 
> I read this page:
> http://www.haskell.org/haskellwiki/Performance/Strictness but I didn't
> get it so I'm asking here: How do I make the function 'measure' to
> actually force the evaluation of (f p)?

You have to make the getting of the second time value depend on (f p) - and 
in such a way that f p is completely evaluated. One way would be to print 
the result between the gettings of time, but for certain tasks, the 
printing may take a substantial amount of time relative to the function you 
want to time, so other dependencies would have to be introduced.
Also, printing values may not be desirable, so you can introduce artificial 
(data) dependencies with seq, pseq and such.
For sorting lists, evaluating the last element or the length forces 
complete evaluation, so [see below]

> 
> Thanks,
> ovidiu
> 
> See the code below:
> ------------------------------------
> module Main where
> import Prelude
> import Data.List
> import Data.Time.Clock
> import System.Random
> 
> quickSort [] = []
> quickSort (x:xs) = (quickSort small) ++ [x] ++ (quickSort big)
>         where
>             small = [p | p <- xs, p <= x]
>             big = [p | p <- xs, p > x]
> 
> randomlist :: Int -> StdGen -> [Int]
> randomlist n = take n . unfoldr (Just . random)
> 
> len = 10 ^ 10
> 
> measure f p = do
>     t1 <- getCurrentTime
>     let sorted = f p
          len = length sorted
> --     t2 <- getCurrentTime
      t2 <- len `seq` getCurrentTime

-- we made the action to get the second time depend on len, so the list has 
to be sorted before the action can be performed.

>     let diff = diffUTCTime t2 t1
>     return diff

However, for timing such functions, wall-clock time is not good, better is 
measuring CPU-time, so use System.CPUTime.getCPUTime instead of 
getCurrentTime (needs some adaption of other code).

> 
> main = do
>     seed  <- newStdGen
>     let rs = randomlist len seed
> 
>     putStrLn $ "Sorting " ++ (show len) ++ " elements..."
> 
>     t <- measure quickSort rs
> 
>     putStrLn $ "Time elapsed: " ++ (show t)





------------------------------

Message: 5
Date: Wed, 3 Aug 2011 00:40:02 +0300
From: Ovidiu Deac <[email protected]>
Subject: Re: [Haskell-beginners] force strict expression evaluation
To: Daniel Fischer <[email protected]>,
        [email protected]
Cc: [email protected]
Message-ID:
        <cakvse7veufveosi3wqm6dr0owcpidoawmslo4rbox9+ao65...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Thanks! Now I have this:

time = do
    t ?  getCurrentTime
    c ?  getCPUTime
    return (t,c)

measure f p = do
   (t1, c1) ?  time
   evaluate $  rnf $ f p
   (t2, c2) ?  time
   return (diffUTCTime t2 t1, c2 - c1)


On Tue, Aug 2, 2011 at 7:16 PM, Daniel Fischer
<[email protected]> wrote:
> On Tuesday 02 August 2011, 17:58:59, Ovidiu Deac wrote:
>> I'm trying to do some performance evaluation and I'm stuck with the
>> fact that Haskell's lazy evaluation - which make my sort extremely
>> fast :)
>
> Well, *I* can do nothing even faster ;)
>
>>
>> I read this page:
>> http://www.haskell.org/haskellwiki/Performance/Strictness but I didn't
>> get it so I'm asking here: How do I make the function 'measure' to
>> actually force the evaluation of (f p)?
>
> You have to make the getting of the second time value depend on (f p) - and
> in such a way that f p is completely evaluated. One way would be to print
> the result between the gettings of time, but for certain tasks, the
> printing may take a substantial amount of time relative to the function you
> want to time, so other dependencies would have to be introduced.
> Also, printing values may not be desirable, so you can introduce artificial
> (data) dependencies with seq, pseq and such.
> For sorting lists, evaluating the last element or the length forces
> complete evaluation, so [see below]
>
>>
>> Thanks,
>> ovidiu
>>
>> See the code below:
>> ------------------------------------
>> module Main where
>> import Prelude
>> import Data.List
>> import Data.Time.Clock
>> import System.Random
>>
>> quickSort [] = []
>> quickSort (x:xs) = (quickSort small) ++ [x] ++ (quickSort big)
>> ? ? ? ? where
>> ? ? ? ? ? ? small = [p | p <- xs, p <= x]
>> ? ? ? ? ? ? big = [p | p <- xs, p > x]
>>
>> randomlist :: Int -> StdGen -> [Int]
>> randomlist n = take n . unfoldr (Just . random)
>>
>> len = 10 ^ 10
>>
>> measure f p = do
>> ? ? t1 <- getCurrentTime
>> ? ? let sorted = f p
> ? ? ? ? ?len = length sorted
>> -- ? ? t2 <- getCurrentTime
> ? ? ?t2 <- len `seq` getCurrentTime
>
> -- we made the action to get the second time depend on len, so the list has
> to be sorted before the action can be performed.
>
>> ? ? let diff = diffUTCTime t2 t1
>> ? ? return diff
>
> However, for timing such functions, wall-clock time is not good, better is
> measuring CPU-time, so use System.CPUTime.getCPUTime instead of
> getCurrentTime (needs some adaption of other code).
>
>>
>> main = do
>> ? ? seed ?<- newStdGen
>> ? ? let rs = randomlist len seed
>>
>> ? ? putStrLn $ "Sorting " ++ (show len) ++ " elements..."
>>
>> ? ? t <- measure quickSort rs
>>
>> ? ? putStrLn $ "Time elapsed: " ++ (show t)
>
>
>



------------------------------

Message: 6
Date: Wed, 3 Aug 2011 01:44:03 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] parallel quicksort
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7sq0kyY3=w1i4dE=nzhpi1xdsnyzj1maq+es3jtcdb...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

I'm trying to write a parallel quicksort algorithm for lists.

This is my original implementation:

quickSort [] = []
quickSort (x:xs) = (quickSort small) ? [x] ? (quickSort big)
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

and the output is:

$ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 -RTS
[1 of 1] Compiling Main             ( quicksort.hs, quicksort.o )
Linking quicksort ...
Sorting 1000000 elements...
CPU Time: 13290000000000
Time elapsed: 8.929503s

ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
quicksort.hs && ./quicksort +RTS -N2 -RTS
Sorting 1000000 elements...
CPU Time: 11240000000000
Time elapsed: 7.785293s

ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
quicksort.hs && ./quicksort +RTS -N1 -RTS
Sorting 1000000 elements...
CPU Time: 6790000000000
Time elapsed: 6.817648s

ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
quicksort.hs && ./quicksort +RTS -N1 -RTS
Sorting 1000000 elements...
CPU Time: 6980000000000
Time elapsed: 7.006658s

ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
quicksort.hs && ./quicksort +RTS -N1 -RTS
Sorting 1000000 elements...
CPU Time: 5900000000000
Time elapsed: 5.932236s

...so the conclusion is that using option N1 is faster the N2. This makes sense.

Then I tried to parallelize it:

First try:
-----------------
quickSort [] = []
quickSort (x:xs) = small `pseq` ((quickSort small) ? [x] ? (quickSort big))
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

$ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 -RTS
[1 of 1] Compiling Main             ( quicksort.hs, quicksort.o )
Linking quicksort ...
Sorting 1000000 elements...
CPU Time: 12020000000000
Time elapsed: 8.29653s

This is slower then the non-parallel version

Second try:
---------------
quickSort [] = []
quickSort (x:xs) = small `par` ((quickSort small) ? [x] ? (quickSort big))
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

$ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 -RTS
Sorting 1000000 elements...
CPU Time: 14750000000000
Time elapsed: 10.772271s

Even slower

Third try:
-------------
quickSort [] = []
quickSort (x:xs) = small `par` (big `par` ((quickSort small) ? [x] ?
(quickSort big)))
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

$ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 -RTS
[1 of 1] Compiling Main             ( quicksort.hs, quicksort.o )
Linking quicksort ...
Sorting 1000000 elements...
CPU Time: 134490000000000
Time elapsed: 122.917093s

Fourth try:
------------------------
quickSort [] = []
quickSort (x:xs) = small `par` (big `pseq` ((quickSort small) ? [x] ?
(quickSort big)))
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

$ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 -RTS
[1 of 1] Compiling Main             ( quicksort.hs, quicksort.o )
Linking quicksort ...
Sorting 1000000 elements...
CPU Time: 12770000000000
Time elapsed: 8.844304s
-----------------------------

It seems that I'm unable to make it parallel. What am I doing wrong?

Thanks,
ovidiu


See the full code below:
--------------------------------------------------
module Main where

import Prelude
import Data.List
import Data.Time.Clock
import System.CPUTime
import System.Random
import Control.Parallel
import Control.Exception (evaluate)
import Control.DeepSeq (rnf)
import Text.Printf

quickSort [] = []
quickSort (x:xs) = small `par` (big `par` ((quickSort small) ? [x] ?
(quickSort big)))
       where
           small = [p | p ?  xs, p ? x]
           big = [p | p ?  xs, p > x]

randomlist :: Int ?  StdGen ?  [Int]
randomlist n = take n?unfoldr (Just?random)

len = 10 ? 6

time = do
    t ?  getCurrentTime
    c ?  getCPUTime
    return (t,c)

measure f p = do
   (t1, c1) ?  time
   evaluate $ rnf $ f p
   (t2, c2) ?  time
   return (diffUTCTime t2 t1, c2 - c1)

main = do
   seed  ?  newStdGen
   let rs = randomlist len seed

   printf "Sorting %d elements...\n" len


   (t, cpu) ?  measure quickSort rs
   printf "CPU Time: %d?nTime elapsed: %s?n" cpu (show t)



------------------------------

Message: 7
Date: Wed, 3 Aug 2011 10:21:23 +0300
From: Ovidiu Deac <[email protected]>
Subject: Re: [Haskell-beginners] parallel quicksort
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7s0K01XfaT2J61M=8evvY6ptDL6usFeF=9ezheoy7r...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Meanwhile I found this chapter
http://book.realworldhaskell.org/read/concurrent-and-multicore-programming.html
which discusses exactly the parallelization of the quicksort
algorithm.

Also this 
http://stackoverflow.com/questions/2338850/haskell-as-a-highly-concurrent-server
which has links to some resources for parallel haskell.

On Wed, Aug 3, 2011 at 1:44 AM, Ovidiu Deac <[email protected]> wrote:
> I'm trying to write a parallel quicksort algorithm for lists.
>
> This is my original implementation:
>
> quickSort [] = []
> quickSort (x:xs) = (quickSort small) ? [x] ? (quickSort big)
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> and the output is:
>
> $ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 
> -RTS
> [1 of 1] Compiling Main ? ? ? ? ? ? ( quicksort.hs, quicksort.o )
> Linking quicksort ...
> Sorting 1000000 elements...
> CPU Time: 13290000000000
> Time elapsed: 8.929503s
>
> ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
> quicksort.hs && ./quicksort +RTS -N2 -RTS
> Sorting 1000000 elements...
> CPU Time: 11240000000000
> Time elapsed: 7.785293s
>
> ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
> quicksort.hs && ./quicksort +RTS -N1 -RTS
> Sorting 1000000 elements...
> CPU Time: 6790000000000
> Time elapsed: 6.817648s
>
> ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
> quicksort.hs && ./quicksort +RTS -N1 -RTS
> Sorting 1000000 elements...
> CPU Time: 6980000000000
> Time elapsed: 7.006658s
>
> ovidiu@asus:~/work/haskell$ ghc -threaded -rtsopts -o quicksort
> quicksort.hs && ./quicksort +RTS -N1 -RTS
> Sorting 1000000 elements...
> CPU Time: 5900000000000
> Time elapsed: 5.932236s
>
> ...so the conclusion is that using option N1 is faster the N2. This makes 
> sense.
>
> Then I tried to parallelize it:
>
> First try:
> -----------------
> quickSort [] = []
> quickSort (x:xs) = small `pseq` ((quickSort small) ? [x] ? (quickSort big))
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> $ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 
> -RTS
> [1 of 1] Compiling Main ? ? ? ? ? ? ( quicksort.hs, quicksort.o )
> Linking quicksort ...
> Sorting 1000000 elements...
> CPU Time: 12020000000000
> Time elapsed: 8.29653s
>
> This is slower then the non-parallel version
>
> Second try:
> ---------------
> quickSort [] = []
> quickSort (x:xs) = small `par` ((quickSort small) ? [x] ? (quickSort big))
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> $ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 
> -RTS
> Sorting 1000000 elements...
> CPU Time: 14750000000000
> Time elapsed: 10.772271s
>
> Even slower
>
> Third try:
> -------------
> quickSort [] = []
> quickSort (x:xs) = small `par` (big `par` ((quickSort small) ? [x] ?
> (quickSort big)))
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> $ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 
> -RTS
> [1 of 1] Compiling Main ? ? ? ? ? ? ( quicksort.hs, quicksort.o )
> Linking quicksort ...
> Sorting 1000000 elements...
> CPU Time: 134490000000000
> Time elapsed: 122.917093s
>
> Fourth try:
> ------------------------
> quickSort [] = []
> quickSort (x:xs) = small `par` (big `pseq` ((quickSort small) ? [x] ?
> (quickSort big)))
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> $ ghc -threaded -rtsopts -o quicksort quicksort.hs && ./quicksort +RTS -N2 
> -RTS
> [1 of 1] Compiling Main ? ? ? ? ? ? ( quicksort.hs, quicksort.o )
> Linking quicksort ...
> Sorting 1000000 elements...
> CPU Time: 12770000000000
> Time elapsed: 8.844304s
> -----------------------------
>
> It seems that I'm unable to make it parallel. What am I doing wrong?
>
> Thanks,
> ovidiu
>
>
> See the full code below:
> --------------------------------------------------
> module Main where
>
> import Prelude
> import Data.List
> import Data.Time.Clock
> import System.CPUTime
> import System.Random
> import Control.Parallel
> import Control.Exception (evaluate)
> import Control.DeepSeq (rnf)
> import Text.Printf
>
> quickSort [] = []
> quickSort (x:xs) = small `par` (big `par` ((quickSort small) ? [x] ?
> (quickSort big)))
> ? ? ? where
> ? ? ? ? ? small = [p | p ? ?xs, p ? x]
> ? ? ? ? ? big = [p | p ? ?xs, p > x]
>
> randomlist :: Int ? ?StdGen ? ?[Int]
> randomlist n = take n?unfoldr (Just?random)
>
> len = 10 ? 6
>
> time = do
> ? ?t ? ?getCurrentTime
> ? ?c ? ?getCPUTime
> ? ?return (t,c)
>
> measure f p = do
> ? (t1, c1) ? ?time
> ? evaluate $ rnf $ f p
> ? (t2, c2) ? ?time
> ? return (diffUTCTime t2 t1, c2 - c1)
>
> main = do
> ? seed ?? ?newStdGen
> ? let rs = randomlist len seed
>
> ? printf "Sorting %d elements...\n" len
>
>
> ? (t, cpu) ? ?measure quickSort rs
> ? printf "CPU Time: %d?nTime elapsed: %s?n" cpu (show t)
>



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 38, Issue 4
****************************************

Reply via email to