yesod / yesod-core / widget-benchmark

main = defaultMain
    [ bench "bigTable html" $ nf bigTableHtml bigTableData
    , bench "bigTable hamlet" $ nf bigTableHamlet bigTableData
    , bench "bigTable widget" $ nf bigTableWidget bigTableData
    , bench "bigTable blaze" $ nf bigTableBlaze bigTableData
    ]
  where
    rows :: Int
    rows = 1000
 
    bigTableData :: [[Int]]
    bigTableData = replicate rows [1..10]
    {-# NOINLINE bigTableData #-}
 
bigTableHtml rows = L.length $ renderHtml [$hamlet|
<table
    $forall row <- rows
        <tr
            $forall cell <- row
                <td>#{show cell}
|]
 
bigTableHamlet rows = L.length $ renderHamlet id [$hamlet|
<table
    $forall row <- rows
        <tr
            $forall cell <- row
                <td>#{show cell}
|]
 
bigTableWidget rows = L.length $ renderHtml $ (run [$hamlet|
<table
    $forall row <- rows
        <tr
            $forall cell <- row
                <td>#{show cell}
|]) (\_ _ -> "foo")
  where
  run (GWidget w) =
    let (_, _, GWData (Body x) _ _ _ _ _ _) = runRWS w () 0
     in x
  {-
  run (GWidget w) = runIdentity $ do
    w' <- flip evalStateT 0
        $ runWriterT $ runWriterT $ runWriterT $ runWriterT
        $ runWriterT $ runWriterT $ runWriterT w
    let ((((((((),
         Body body),
         _),
         _),
         _),
         _),
         _),
         _) = w'
 
    return body
    -}
 
bigTableBlaze t = L.length $ renderHtml $ table $ mconcat $ map row t
  where
    row r = tr $ mconcat $ map (td . string . show) r

 

posted @ 2012-03-19 11:41  kelby  阅读(218)  评论(0编辑  收藏  举报