[Haskell-beginners] Storing the time difference between two Monotonic time results

David McBride toad3k at gmail.com
Mon Jul 9 19:52:00 UTC 2018


toList :: String -> [Integer]
toList input = read ("[" ++ input ++ "]")

That was creative, but a more idiomatic way to write that would be to use
the split package

stack ghci --package split

import Data.Split

toList :: String -> [Integer]
toList = fmap read . splitOn ","

As for how to aggregate times, start thinking about the types before you
start.  Each iteration you should take a List and return a TimeSpec.

testcase :: [Integer] -> IO TimeSpec
testcase l = do
  start <- getTime Monotonic
  evaluate (mergesort l)
  end <- getTime Monotonic
  return $ diffTimeSpec start end

Now, we need to run it 100 times and collect the times.  Sounds like a job
for map, but since testcase is monadic, use mapM instead (or traverse).

main = do
  unsortedList <- undefined
  times <- mapM (\c -> testcase unsortedList) [1..100]
  print times

On Mon, Jul 9, 2018 at 9:40 AM, Awsaf Rahman <awsafrahman1704 at gmail.com>
wrote:

> Okay, here is what I am trying to do. I am trying to time this mergesort
> program. I want to run this program 100, 1000, etc times and store the
> timing results in a list if possible. Can't seem to figure out how to do
> it! The following program prints the timings to the shell and I need to
> figure out a way to store the timings in a list.
>
>
> {-# LANGUAGE OverloadedStrings #-}
> {-# LANGUAGE BangPatterns #-}
> import Control.Exception
> import Formatting
> import Formatting.Clock
> import System.Clock
> import Control.DeepSeq
>
> mergesort [] = []
> mergesort [x] = [x]
> mergesort xs = let (lhalf, rhalf) = splitAt (length xs `div` 2) xs
>                in merge' (mergesort lhalf) (mergesort rhalf)
>
> merge' lhalf rhalf = merge lhalf rhalf []
>
> merge [] [] acc = reverse acc
> merge [] y acc = reverse acc ++ y
> merge x [] acc = reverse acc ++ x
>
> merge (l:ls) (r:rs) acc
>         | l < r = merge ls (r:rs) (l:acc)
>         | otherwise = merge rs (l:ls) (r:acc)
>
> toList :: String -> [Integer]
> toList input = read ("[" ++ input ++ "]")
>
> repeater unsortedlist 0 result = return (result)
>
> repeater unsortedlist counter result = do
>                             start <- getTime Monotonic
>                             evaluate(mergesort unsortedlist)
>                             end <- getTime Monotonic
>                             fprint (timeSpecs % "\n") start end
>                             repeater unsortedlist (counter-1) result
>
> main = do
>     file <- getLine
>     contents <- readFile file
>     let !unsortedlist = (toList contents)
>     repeater unsortedlist 100 []
>
>
> On Mon, Jul 9, 2018 at 3:21 PM, David McBride <toad3k at gmail.com> wrote:
>
>> I guess whatever version you are using did not export that function.  In
>> any case the definition for that function is incredibly simple, so you
>> could just write your own for now.
>>
>> diffTimeSpec :: TimeSpec -> TimeSpec -> TimeSpecdiffTimeSpec ts1 ts2 = abs (ts1 - ts2)
>>
>>
>>
>> On Mon, Jul 9, 2018 at 9:08 AM, Awsaf Rahman <awsafrahman1704 at gmail.com>
>> wrote:
>>
>>> I imported the System.Clock module and tried to use the diffTimeSpec
>>> function but it keeps saying "out of scope".
>>>
>>> On Mon, Jul 9, 2018 at 2:45 PM, David McBride <toad3k at gmail.com> wrote:
>>>
>>>> There is a diffTimeSpec function in that module that seems like it
>>>> would work.
>>>>
>>>> On Mon, Jul 9, 2018 at 8:30 AM, Awsaf Rahman <awsafrahman1704 at gmail.com
>>>> > wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I am trying to time a function I have written in haskell using the
>>>>> clock package in the following way:
>>>>>
>>>>> *start <- getTime Monotonic*
>>>>> *evaluate(something)*
>>>>> *end <- getTime Monotonic*
>>>>> *fprint (timeSpecs % "\n") start end*
>>>>>
>>>>>
>>>>> Now what I want is to store the time difference between *start* and *end.
>>>>> *Is there a way I can do that?
>>>>>
>>>>> Regards
>>>>> Awsaf
>>>>>
>>>>> _______________________________________________
>>>>> Beginners mailing list
>>>>> Beginners at haskell.org
>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> Beginners at haskell.org
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180709/ece38705/attachment.html>


More information about the Beginners mailing list