quot向零舍入,div向负无穷大舍入:
div (-3) 2 == (-2)
quot (-3) 2 == (-1)
Run Code Online (Sandbox Code Playgroud)
至于开销div,quot有一个相应的原始GHC操作,同时div做一些额外的工作:
quotRemInt :: Int -> Int -> (Int, Int)
(I# x) `quotRemInt` (I# y) = case x `quotRemInt#` y of
(# q, r #) ->
(I# q, I# r)
divModInt# :: Int# -> Int# -> (# Int#, Int# #)
x# `divModInt#` y#
| (x# ># 0#) && (y# <# 0#) = case (x# -# 1#) `quotRemInt#` y# of
(# q, r #) -> (# q -# 1#, r +# y# +# 1# #)
| (x# <# 0#) && (y# ># 0#) = case (x# +# 1#) `quotRemInt#` y# of
(# q, r #) -> (# q -# 1#, r +# y# -# 1# #)
| otherwise = x# `quotRemInt#` y#
Run Code Online (Sandbox Code Playgroud)
a `quot` b
| b == 0 = divZeroError
| b == (-1) && a == minBound = overflowError -- Note [Order of tests]
-- in GHC.Int
| otherwise = a `quotInt` b
a `div` b
| b == 0 = divZeroError
| b == (-1) && a == minBound = overflowError -- Note [Order of tests]
-- in GHC.Int
| otherwise = a `divInt` b
Run Code Online (Sandbox Code Playgroud)
我还做了一小部分微基准测试,但它应该用大量的盐,因为GHC和LLVM优化紧密的数字代码,就像没有明天一样.我试图阻挠他们,结果似乎是现实:14,67毫秒为div和13,37毫秒的quot.此外,它是带-O2和-fllvm的GHC 7.8.2.这是代码:
{-# LANGUAGE BangPatterns #-}
import Criterion.Main
import System.Random
benchOp :: (Int -> Int) -> Int -> ()
benchOp f = go 0 0 where
go !i !acc !limit | i < limit = go (i + 1) (f i) limit
| otherwise = ()
main = do
limit1 <- randomRIO (1000000, 1000000 :: Int)
limit2 <- randomRIO (1000000, 1000000 :: Int)
n <- randomRIO (100, 100 :: Int)
defaultMain [
bench "div" $ whnf (benchOp (`div` n)) limit1,
bench "quot" $ whnf (benchOp (`quot` n)) limit2]
Run Code Online (Sandbox Code Playgroud)