Changes between Version 12 and Version 13 of PrimBool


Ignore:
Timestamp:
Mar 18, 2013 8:23:24 AM (13 months ago)
Author:
jstolarek
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PrimBool

    v12 v13  
    309309=== Benchmarks for the proposed patch === 
    310310 
    311 Below is a proof-of-concept filter function that demonstrates performance gains possible with the new primops: 
    312  
    313 {{{ 
    314 filterN :: Vector Int -> Vector Int 
     311Below is a benchmark for the proof-of-concept filter function that demonstrates performance gains possible with the new primops: 
     312 
     313{{{ 
     314{-# LANGUAGE BangPatterns, MagicHash #-} 
     315module Main ( 
     316             main 
     317            ) where 
     318 
     319import Control.Monad.ST                  (runST) 
     320import Criterion.Config                  (Config, cfgPerformGC, 
     321                                          defaultConfig, ljust) 
     322import Criterion.Main 
     323import Data.Vector.Unboxed.Mutable       (unsafeNew, unsafeSlice, unsafeWrite) 
     324import Data.Vector.Unboxed               as U (Vector, filter, foldM', 
     325                                               fromList, length, unsafeFreeze) 
     326import GHC.Exts                          (Int (I#), (.>=#)) 
     327import System.Random                     (RandomGen, mkStdGen, randoms) 
     328import Prelude                    hiding (filter, length) 
     329 
     330 
     331filterN :: U.Vector Int -> U.Vector Int 
    315332filterN vec = runST $ do 
    316333  let !size = length vec 
     
    323340  fSize <- foldM' put 0 vec 
    324341  unsafeFreeze $ unsafeSlice 0 fSize fVec 
    325 }}} 
    326  
    327 Benchmarking with criterion shows that this function is 60% faster than the filter function based on stream fusion (tested for unboxed vectors containing 10 thousand and 10 million elements). 
     342 
     343 
     344main :: IO () 
     345main = return (mkStdGen 1232134332) >>= 
     346       defaultMainWith benchConfig (return ()) . benchmarks 
     347 
     348 
     349benchmarks :: RandomGen g => g -> [Benchmark] 
     350benchmarks gen = 
     351    let dataSize   = 10 ^ (7 :: Int) 
     352        inputList  = take dataSize . randoms $ gen :: [Int] 
     353        inputVec   = fromList inputList 
     354        isPositive = (> 0) 
     355    in [ 
     356       bgroup "Filter" 
     357         [ 
     358           bench "New"    $ whnf (filterN)            inputVec 
     359         , bench "Vector" $ whnf (filter  isPositive) inputVec 
     360         ] 
     361      ] 
     362 
     363 
     364benchConfig :: Config 
     365benchConfig = defaultConfig { 
     366             cfgPerformGC = ljust True 
     367           } 
     368 
     369}}} 
     370 
     371Compile and run with: 
     372 
     373{{{ 
     374ghc -O2 -fllvm -optlo-O3 Main.hs 
     375./Main -o report.html 
     376}}} 
     377 
     378Benchmarking shows that `filterN` function is 60% faster than the `filter` function based on stream fusion (tested for unboxed vectors containing 10 thousand and 10 million elements).