Commits

Bryan O'Sullivan committed 1208636

Benchmark more functions.

Comments (0)

Files changed (1)

benchmarks/Simple.hs

 {-# LANGUAGE BangPatterns, OverloadedStrings #-}
 
+module Main (main) where
+
 import Control.Monad
 import System.Environment
-import qualified Data.Text.Format as T
+import Data.Text.Format as T
 import Data.Time.Clock
+import Data.Text.Lazy.Encoding
+import qualified Data.ByteString.Lazy as L
+import System.IO
+
+counting :: Int -> (Int -> () -> IO ()) -> IO ()
+counting count act = loop 0
+    where loop !i | i < count = act i () >> loop (i+1)
+                  | otherwise = return ()
+{-# NOINLINE counting #-}
+  
+idle count = counting count $ \_ x -> return ()
+
+plain count = counting count $ \_ x -> do
+  L.putStr . encodeUtf8 $ "hi mom\n"
+
+unit count = counting count $ \_ x -> do
+  let t = T.format "hi mom\n" x
+  L.putStr . encodeUtf8 $ t
+
+int count = counting count $ \i x -> do
+  let t = T.format "hi mom {}\n" (Only i)
+  L.putStr . encodeUtf8 $ t
+
+double count = counting count $ \i x -> do
+  let t = T.format "hi mom {}\n" (Only (fromIntegral i * pi::Double))
+  L.putStr . encodeUtf8 $ t
 
 main = do
   args <- getArgs
   let count = case args of
-                (x:_) -> read x :: Int
-                _     -> 100000
+                (_:x:_) -> read x
+                _       -> 100000
+  let bm = case args of
+             ("idle":_)   -> idle
+             ("plain":_)  -> plain
+             ("unit":_)   -> unit
+             ("double":_) -> double
+             ("int":_)    -> int
+             _            -> error "wut?"
   start <- getCurrentTime
-  forM_ [0..count] $ \i -> do
-    let !t = T.format "hi mom {}\n" [fromIntegral i * pi::Double]
-    return ()
+  bm count
   elapsed <- (`diffUTCTime` start) `fmap` getCurrentTime
-  T.print "{} iterations in {} secs ({} thousand/sec\n"
+  T.hprint stderr "{} iterations in {} secs ({} thousand/sec)\n"
        (count, elapsed,
         fromRational (toRational count / toRational elapsed / 1e3) :: Double)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.