packages feed

o-clock-1.0.0: benchmark/Main.hs

{-# OPTIONS_GHC -fno-warn-orphans #-}

{-# LANGUAGE CPP                        #-}
{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures             #-}
{-# LANGUAGE StandaloneDeriving         #-}
{-# LANGUAGE TypeApplications           #-}

module Main where

#ifdef NO_deepseq
import Control.DeepSeq (NFData)
#endif
import Gauge (bench, bgroup, defaultMain, nf, whnf)

#ifdef NO_deepseq
import Time (Rat, Time)
#endif
import Time (Day, Hour, Microsecond, Nanosecond, Second, hour, mcs, ns, sec, toUnit, week)

import qualified Data.Time.Units as TU (Day, Hour, Microsecond, Nanosecond, Second, Week,
                                        convertUnit)
import qualified Tiempo (hours, microSeconds, toHours, toMicroSeconds)


#ifdef NO_deepseq
instance NFData (Time (unit :: Rat))
#endif

main :: IO ()
main = defaultMain
    [ bgroup "Second to Nanosecond"
        [ bench "o'clock"    $ nf   (toUnit @Nanosecond . sec) 1
        , bench "time-units" $ whnf (TU.convertUnit :: TU.Second -> TU.Nanosecond) 1
        ]
    , bgroup "Hour to Microsecond"
        [ bench "o'clock"    $ nf   (toUnit @Microsecond . hour) 1
        , bench "time-units" $ whnf (TU.convertUnit :: TU.Hour -> TU.Microsecond) 1
        , bench "tiempo"     $ nf   (Tiempo.toMicroSeconds . Tiempo.hours) 1
        ]
    , bgroup "3600000000 Microsecond to Hours"
        [ bench "o'clock"    $ nf   (toUnit @Hour . mcs) 3600000000
        , bench "time-units" $ whnf (TU.convertUnit :: TU.Microsecond -> TU.Hour) 3600000000
        , bench "tiempo"     $ nf   (Tiempo.toHours . Tiempo.microSeconds) 3600000000
        ]
    , bgroup "1000ns to s"
        [ bench "o'clock"    $ nf   (toUnit @Second . ns) 1000
        , bench "time-units" $ whnf (TU.convertUnit :: TU.Nanosecond -> TU.Second) 1000
        ]
    , bgroup "week to days"
        [ bench "o'clock"    $ nf   (toUnit @Day . week ) 1
        , bench "time-units" $ whnf (TU.convertUnit :: TU.Week -> TU.Day) 1
        ]
    ]