Skip to content

Instantly share code, notes, and snippets.

@autotaker
Created April 4, 2016 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save autotaker/ecc5317d3f07cc2890f68da58bdec379 to your computer and use it in GitHub Desktop.
Save autotaker/ecc5317d3f07cc2890f68da58bdec379 to your computer and use it in GitHub Desktop.
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Control.DeepSeq
import Data.Time
import Control.Exception(evaluate)
import System.IO
import Control.Applicative
import Text.Printf
import Prelude hiding(log)
import qualified VecSort as Vec
import qualified Data.List as List
import Control.Monad
log :: String -> IO UTCTime
log xs = do
t <- getCurrentTime
hPrintf stderr "[%s] %s\n" (show t) xs
return t
main :: IO ()
main = do
-- 入力読み込み
t0 <- log "Parsing: begin"
xs <- map (fst . fromJust . B.readInt) . B.words <$> B.getContents
-- 整数リストを評価(パーサのサンクを潰すため)
evaluate (rnf xs)
t1 <- log "Parsing: end"
let dt_parsing = diffUTCTime t1 t0
void $ log $ printf "Parsing: %s" (show dt_parsing)
-- ソート
[(l1, dt1), (l2, dt2)] <- forM [(Vec.sort, "Vector"), (List.sort, "List")] $ \(sortFunc, modName) -> do
t_sort_begin <- log $ modName ++ ": begin"
let ls = sortFunc xs
evaluate (rnf ls)
t_sort_end <- log $ modName ++ ": end"
let dt_sorting = diffUTCTime t_sort_end t_sort_begin
_ <- log $ printf "%s: Sorting: %s" modName (show dt_sorting)
return (ls,dt_sorting)
-- 結果が正しいかチェック
void $ log "result validation begin"
if l1 /= l2 then
void $ log "Wrong Answer!!"
else
void $ log "Correct!"
-- 結果を出力
putStr $ unlines $ map show l1
t3 <- log "output done"
let dt_total = diffUTCTime t3 t0
_ <- log $ printf "Elapsed Time: %s" (show dt_total)
_ <- log $ printf "Speedup (Data.List.sort / VecSort.sort) = %d%%" (round (dt2 / dt1 * 100) :: Int)
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment