Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. {-# LANGUAGE BangPatterns #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3. {-# LANGUAGE DeriveAnyClass #-}
  4. module Main where
  5.  
  6. import Prelude as P
  7.  
  8. import Criterion.Main
  9. import Control.DeepSeq
  10. import Data.Monoids
  11. import Foreign.Storable (Storable, alignment, peek,
  12. peekByteOff, poke, pokeByteOff,
  13. sizeOf)
  14. import GHC.Generics (Generic)
  15. import System.Random
  16.  
  17. import qualified Data.Vector.Storable as SVec
  18. import Data.Vector.Storable (Vector)
  19. import qualified Data.Vector.Storable.Mutable as Vector
  20. import Data.Vector.Storable.Mutable (IOVector)
  21.  
  22.  
  23. data Foo = Foo Int Int deriving (Show, Eq, Generic, NFData)
  24.  
  25. chunkSize :: Int
  26. chunkSize = sizeOf (undefined :: Int)
  27. {-# INLINE chunkSize #-}
  28.  
  29. instance Storable Foo where
  30. sizeOf _ = 2 * chunkSize ; {-# INLINE sizeOf #-}
  31. alignment _ = chunkSize ; {-# INLINE alignment #-}
  32. peek ptr = Foo
  33. <$> peekByteOff ptr 0
  34. <*> peekByteOff ptr chunkSize
  35. {-# INLINE peek #-}
  36. poke ptr (Foo a b) = do
  37. pokeByteOff ptr 0 a
  38. pokeByteOff ptr chunkSize b
  39. {-# INLINE poke #-}
  40.  
  41. -- create an empty vector
  42. mkFooVec :: Int -> IO (Vector Foo)
  43. mkFooVec !i = SVec.unsafeFreeze =<< Vector.new (i + 1)
  44.  
  45. -- fill the vector with sample structs
  46. populateFooVec :: Int -> Vector Foo -> IO (Vector Foo)
  47. populateFooVec !i !v = do
  48. v' <- SVec.unsafeThaw v
  49. let go 0 = return ()
  50. go j = Vector.unsafeWrite v' j (Foo j $ j + 1) >> go (j - 1)
  51. go i
  52. SVec.unsafeFreeze v'
  53.  
  54. -- trying to force the evaluation by accessing random elements
  55. populateAndAccess :: Int -> Vector Foo -> IO Foo
  56. populateAndAccess !i !v = do
  57. vec <- populateFooVec i v
  58. idx <- getStdRandom (randomR (0, i - 1))
  59. return $ SVec.unsafeIndex vec idx
  60.  
  61. -- trying once again, this time printing the elements to the console
  62. accessAndPrint :: Int -> Vector Foo -> IO ()
  63. accessAndPrint !i !v = do
  64. foo <- populateAndAccess i v
  65. print foo
  66.  
  67. main :: IO ()
  68. main = do
  69. defaultMain [
  70. bgroup "Storable vector (mutable)"
  71. $ (\(i :: Int) -> env (mkFooVec (10 ^ i))
  72. $ \v -> bench ("10e" <> show i)
  73. $ nfIO (populateFooVec (10 ^ i) v)) <$> [6..8]
  74. , bgroup "Storable vector with index (mutable)"
  75. $ (\(i :: Int) -> env (mkFooVec (10 ^ i))
  76. $ \v -> bench ("10e" <> show i)
  77. $ nfIO (populateAndAccess (10 ^ i) v)) <$> [6..8]
  78. , bgroup "Storable vector with index and print (mutable)"
  79. $ (\(i :: Int) -> env (mkFooVec (10 ^ i))
  80. $ \v -> bench ("10e" <> show i)
  81. $ nfIO (accessAndPrint (10 ^ i) v)) <$> [6..8]
  82. ]
Add Comment
Please, Sign In to add comment