Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
time benchmark
import Criterion.Main
import qualified Data.ByteString.Char8 as BS
import Data.Time
import Data.Time.Clock.POSIX
import Data.UnixTime
import Network.HTTP.Date
-- import System.IO
import System.Locale
import System.Posix
main :: IO ()
main = do
et <- epochTime
defaultMain $ [
bench "unix-time" (whnf unixTime et)
, bench "http-date" (whnf httpDate et)
, bench "time" (whnf time et)
]
{-
main :: IO ()
main = do
putStrLn "epochTime" >> hFlush stdout
et <- epochTime
BS.hPutStrLn stdout $ unixTime et
BS.hPutStrLn stdout $ httpDate et
BS.hPutStrLn stdout $ time et
-}
unixTime :: EpochTime -> BS.ByteString
unixTime = formatUnixTimeGMT webDateFormat . fromEpochTime
httpDate :: EpochTime -> BS.ByteString
httpDate = formatHTTPDate . epochTimeToHTTPDate
time :: EpochTime -> BS.ByteString
time = BS.pack . formatTime defaultTimeLocale "%a, %d %b %Y %H:%M:%S GMT" . posixSecondsToUTCTime . realToFrac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment