Skip to content

Commit c9994f1

Browse files
committed
testing: add clock.hs
1 parent 465c1c5 commit c9994f1

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

testing/clock.hs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import IOCP.Clock
2+
3+
import Control.Applicative
4+
import Control.Exception
5+
import Control.Monad
6+
import Data.Maybe
7+
import System.IO.Error
8+
9+
-- From the ansi-terminal package. Used to remove blank lines introduced by
10+
-- user hitting enter to get the next time.
11+
import System.Console.ANSI (cursorUpLine)
12+
13+
handleEOF :: IO () -> IO ()
14+
handleEOF = handleJust (guard . isEOFError) (\_ -> return ())
15+
16+
maybeGetTime :: Maybe Clock -> IO (Maybe Seconds)
17+
maybeGetTime = maybe (return Nothing) (fmap Just . getTime)
18+
19+
formatTimes :: Seconds -> Maybe Seconds -> Maybe Seconds -> String
20+
formatTimes gtc gtc64 qpc =
21+
concat $ map (pad 16)
22+
[ pad 16 $ show gtc
23+
, pad 16 $ maybe "n/a" show gtc64
24+
, pad 25 $ maybe "n/a" show qpc
25+
, maybe "n/a" show $ liftA2 (-) qpc (gtc64 <|> Just gtc)
26+
]
27+
28+
pad :: Int -> String -> String
29+
pad n str = str ++ replicate (n - length str) ' '
30+
31+
main :: IO ()
32+
main = do
33+
gtc <- getTickCount
34+
gtc64 <- getTickCount64
35+
qpc <- queryPerformanceCounter
36+
37+
when (isNothing gtc64) $
38+
putStrLn "GetTickCount64 not available"
39+
when (isNothing qpc) $
40+
putStrLn "QueryPerformanceCounter not available"
41+
42+
let printTimes = liftM3 formatTimes (getTime gtc)
43+
(maybeGetTime gtc64)
44+
(maybeGetTime qpc)
45+
>>= putStrLn
46+
47+
putStrLn ""
48+
putStrLn "GetTickCount GetTickCount64 QueryPerformanceCounter QPC-GTC"
49+
50+
handleEOF $ forever $ do
51+
printTimes
52+
_ <- getLine
53+
cursorUpLine 1

0 commit comments

Comments
 (0)