Skip to content

Commit

Permalink
Extract solution timing function
Browse files Browse the repository at this point in the history
  • Loading branch information
killy971 committed Jan 14, 2015
1 parent 208c965 commit 7f9d94f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/Main.hs
@@ -1,10 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified Data.Map as M
import Formatting
import Formatting.Clock
import ProjectEuler.Problem001
import ProjectEuler.Problem002
import ProjectEuler.Problem004
Expand Down Expand Up @@ -45,7 +41,7 @@ import ProjectEuler.Problem214
import ProjectEuler.Problem225
import System.Environment (getArgs)
import System.Exit (exitSuccess)
import System.Clock
import Util

solutions :: M.Map Integer Integer
solutions = M.fromList [
Expand Down Expand Up @@ -98,13 +94,9 @@ main = do
["--help"] -> usage >> exitSuccess
["-h"] -> usage >> exitSuccess
[number] -> do
let n = read number :: Integer
start <- getTime Monotonic
case solution n of
Just result -> print result
case solution (read number :: Integer) of
Just result -> time result >>= print
Nothing -> putStrLn "There is no solution yet for this problem"
end <- getTime Monotonic
fprint (timeSpecs % "\n") start end
_ -> usage >> exitSuccess
where
usage = putStrLn "Usage: cabal run problem [number]"
12 changes: 12 additions & 0 deletions src/Util.hs
@@ -1,9 +1,14 @@
{-# LANGUAGE OverloadedStrings #-}

module Util where

import Data.List (delete, maximumBy, group, tails, transpose)
import Data.Ord
import Data.Ratio
import qualified Data.Set as Set
import qualified Formatting as F
import Formatting.Clock
import System.Clock

ints :: (Enum t, Num t) => [t]
ints = [1..]
Expand Down Expand Up @@ -109,3 +114,10 @@ countPartitions = (map p' [0..] !!)
where pent x = takeWhile (<= x) $ concatMap f [1..]
where f k = [(m - k) `div` 2, (m + k) `div` 2]
where m = 3 * k * k

time :: a -> IO a
time x = do
start <- getTime Monotonic
end <- x `seq` getTime Monotonic
F.fprint (timeSpecs F.% "\n") start end
return x

0 comments on commit 7f9d94f

Please sign in to comment.