Guest User

criterion bug code

a guest
Jun 9th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import qualified Data.Map as Map
  3. import qualified Data.ByteString.Char8 as BS
  4. import Control.Concurrent.Async
  5. import Criterion.Main
  6.  
  7.  
  8. compterMotDansFichier =
  9.     Map.fromList . map (\x->(head x, length x)) . group . sort . filter (\x->BS.length x > 5) . BS.words
  10. compterMots = Map.unionsWith (+) . map compterMotDansFichier
  11.  
  12. compterMotsLecture noms = do
  13.     fmap (Map.unionsWith (+)) $ mapM (\x->fmap compterMotDansFichier $ BS.readFile x) noms
  14.  
  15. delegerTache _ [] = do return Map.empty
  16. delegerTache n fichiers = do
  17.     let (x,xs) = splitAt n fichiers
  18.     th <- async(delegerTache n xs)
  19.     donneesCourante <- compterMotsLecture x
  20.     result <- wait th
  21.     return $ Map.unionWith (+) donneesCourante result
  22.    
  23. delegerTache2 _ [] = do return Map.empty
  24. delegerTache2 n fichiers = do
  25.     let
  26.         (x,xs) = splitAt n fichiers
  27.         donneesCourante = (compterMots x)
  28.     th <- async(delegerTache2 n xs)
  29.     result <- wait th
  30.     return $ Map.unionWith (+) donneesCourante result
  31.    
  32. arbre n fichiers = do
  33.     if length fichiers > n then do
  34.         let (xs1,xs2) = splitAt ((length fichiers) `quot` 2) fichiers
  35.         th1 <- async (arbre n xs1)
  36.         th2 <- async (arbre n xs2)
  37.         r1 <- wait th1
  38.         r2 <- wait th2
  39.         return $ Map.unionWith (+) r1 r2
  40.     else do
  41.         compterMotsLecture fichiers
  42.  
  43. parMain n xs = do
  44.     dict <- delegerTache n xs
  45.     writeFile "rapport.par.txt" $ show dict
  46.  
  47. parMain2 n xs = do
  48.     fichiers <- mapM BS.readFile xs
  49.     dict <- delegerTache2 n fichiers
  50.     writeFile "rapport.par2.txt" $ show $ dict
  51.    
  52. seqMain xs = do
  53.     dict <- compterMotsLecture xs
  54.     writeFile "rapport.txt" $ show dict
  55.    
  56.    
  57. seqSimple xs = do
  58.     writeFile "rapport.txt" $ show $ compterMots xs
  59.  
  60. parSimple n xs = do
  61.     dict <- delegerTache2 n xs
  62.     writeFile "rapport.par.txt" $ show $ dict
  63.    
  64. arbreMain n xs = do
  65.     dict <- arbre n xs
  66.     writeFile "rapport.arbre.txt" $ show dict
  67.    
  68. buildTest xs =
  69.     let
  70.         n = 1000
  71.         chunkATenter = [2^i|i<-[1..(truncate  $ logBase 2 $ fromIntegral  n)]]
  72.         benchSeq = [bench "sequentiel sans IO" $ whnfIO  $ seqSimple xs]
  73.         benchPar = (map (\i-> bench ("par " ++ show i ) $ whnfIO $ parSimple i xs) chunkATenter)
  74.     in benchSeq ++ benchPar
  75.    
  76.    
  77. main = do
  78.     xs <- mapM BS.readFile fichiers
  79.     defaultMain [bgroup "main" $ buildTest xs ]
  80.     where
  81.         n = 1000
  82.         fichiers = replicate n "test.txt"
  83.         chunkATenter = [2^i|i<-[1..(truncate  $ logBase 2 $ fromIntegral  n)]]
  84.         benchSeq = [bench "sequentiel" $ whnfIO  $ seqMain fichiers]
  85.         benchPar = (map (\i-> bench ("par " ++ show i ) $ whnfIO $ parMain i fichiers) chunkATenter)
  86.         benchPar2 = (map (\i-> bench ("par2 " ++ show i ) $ whnfIO $ parMain2 i fichiers) chunkATenter)
  87.         benchArbre = (map (\i-> bench ("arbre " ++ show i ) $ whnfIO $ arbreMain i fichiers) chunkATenter)
Advertisement
Add Comment
Please, Sign In to add comment