1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
-- |
-- Copyright: (c) 2020-2022 Andrew Lelechenko
-- Licence: BSD3
--
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE TemplateHaskell #-}
module Main (main) where
import GHC.Exts
import Numeric.QuoteQuot
import Test.Tasty.Bench
measureWord :: String -> (Word -> Word) -> Benchmark
measureWord name f = bench name $
whnf (\(W# n#) -> W# (go 0## n#)) 100000000
where
go acc# 0## = acc#
go acc# k# = go (let !(W# fk) = f (W# k#) in acc# `plusWord#` fk) (k# `minusWord#` 1##)
{-# INLINE measureWord #-}
measureInt :: String -> (Int -> Int) -> Benchmark
measureInt name f = bench name $
whnf (\(I# n#) -> I# (go 0# n#)) 100000000
where
go acc# 0# = acc#
go acc# k# = go (let !(I# fk) = f (I# k#) in acc# +# fk) (k# -# 1#)
{-# INLINE measureInt #-}
#define benchWord(n) \
bgroup (show (n :: Word)) \
[ measureWord "quot" (`quot` (n :: Word)) \
, bcompare ("$NF == \"quot\" && $(NF-1) == \"" ++ show (n :: Word) ++ "\" && $(NF-2) == \"Word\"") \
$ measureWord "quoteQuot" $$(quoteQuot (n :: Word)) \
]
#define benchInt(n) \
bgroup (show (n :: Int)) \
[ measureInt "quot" (`quot` (n :: Int)) \
, bcompare ("$NF == \"quot\" && $(NF-1) == \"" ++ show (n :: Int) ++ "\" && $(NF-2) == \"Int\"") \
$ measureInt "quoteQuot" $$(quoteQuot (n :: Int)) \
]
main :: IO ()
main = defaultMain
[ bgroup "Word"
[ benchWord(3)
, benchWord(5)
, benchWord(7)
]
#if MIN_VERSION_base(4,15,0)
, bgroup "Int"
[ benchInt(3)
, benchInt(5)
, benchInt(7)
]
#endif
]
|