Bryan O'Sullivan avatar Bryan O'Sullivan committed f52f3fb

Refactor code a little to simplify it.

Relies on concatU, which is not yet in uvector.

Comments (0)

Files changed (2)

     , runAndAnalyse
     ) where
 
-import Control.Monad ((<=<), forM_, liftM, replicateM_, when)
+import Control.Monad ((<=<), forM_, replicateM_, when)
 import Criterion.Analysis (OutlierVariance(..), classifyOutliers,
                            outlierVariance, noteOutliers)
 import Criterion.Config (Config(..), Plot(..), fromLJ)
 import Criterion.Measurement (getTime, runForAtLeast, secs, time_)
 import Criterion.Plot (plotWith, plotKDE, plotTiming)
 import Criterion.Types (Benchmarkable(..), Benchmark(..), bench, bgroup)
-import Data.Array.Vector ((:*:)(..), lengthU, mapU, foldlU)
-import Data.Maybe (fromMaybe, mapMaybe)
+import Data.Array.Vector ((:*:)(..), concatU, lengthU, mapU)
+import Data.Maybe (fromMaybe)
 import Data.Monoid (getLast)
-import Statistics.Function (createIO)
+import Statistics.Function (createIO, minMax)
 import Statistics.KernelDensity (epanechnikovPDF)
 import Statistics.RandomVariate (withSystemRandom)
 import Statistics.Resampling (resample)
 plotAll cfg descTimes = forM_ descTimes $ \(desc,times) -> do
   plotWith Timing cfg $ \o -> plotTiming o desc times
   plotWith KernelDensity cfg $ \o -> uncurry (plotKDE o desc extremes)
-                                            (epanechnikovPDF 100 times)
+                                             (epanechnikovPDF 100 times)
   where
-    extremes :: Maybe (Double, Double)
-    extremes = foldl minMaxMaybe2 Nothing $ mapMaybe (foldlU minMaxMaybe Nothing . snd) descTimes
-
-    minMaxMaybe :: Maybe (Double, Double) -> Double -> Maybe (Double, Double)
-    minMaxMaybe a b = minMaxMaybe2 a (b, b)
-
-    minMaxMaybe2 :: Maybe (Double, Double) -> (Double, Double) -> Maybe (Double, Double)
-    minMaxMaybe2 Nothing (xMin, xMax) = Just (xMin, xMax)
-    minMaxMaybe2 (Just (curMin, curMax)) (xMin, xMax) = Just (min xMin curMin, max xMax curMax)
+    extremes = toJust . minMax . concatU . map snd $ descTimes
+    toJust r@(lo :*: hi)
+        | lo == infinity || hi == -infinity = Just r
+        | otherwise                         = Nothing
+        where infinity                      = 1/0
 
 
 -- | Run, and analyse, one or more benchmarks.
                                        return []
             | otherwise = return []
             where desc' = prefix pfx desc
-        go pfx (BenchGroup desc bs) = liftM concat $ mapM (go (prefix pfx desc)) bs
+        go pfx (BenchGroup desc bs) = concat `fmap` mapM (go (prefix pfx desc)) bs
         prefix ""  desc = desc
         prefix pfx desc = pfx ++ '/' : desc
 

Criterion/Plot.hs

-{-# LANGUAGE CPP, ScopedTypeVariables #-}
+{-# LANGUAGE CPP, ScopedTypeVariables, TypeOperators #-}
 
 -- |
 -- Module      : Criterion.Plot
 -- | Plot kernel density estimate.
 plotKDE :: PlotOutput           -- ^ The kind of output desired.
         -> String               -- ^ Benchmark name.
-        -> Maybe (Double, Double) -- ^ Minimum extremities for x-axis
+        -> Maybe (Double :*: Double) -- ^ Range of x-axis
         -> Points               -- ^ Points at which KDE was computed.
         -> UArr Double          -- ^ Kernel density estimates.
         -> IO ()
          $ plot_bars_spacing ^= BarsFixGap 0
          $ defaultPlotBars
 
-renderKDE :: String -> Maybe (Double, Double) -> Points -> UArr Double -> Renderable ()
-renderKDE desc possibleOtherExtremities points pdf = toRenderable layout
+renderKDE :: String -> Maybe (Double :*: Double) -> Points -> UArr Double
+          -> Renderable ()
+renderKDE desc exs points pdf = toRenderable layout
   where
     layout = layout1_title ^= "Densities of execution times for \"" ++
                               desc ++ "\""
                $ laxis_title ^= "execution time"
                $ defaultLayoutAxis
 
-    semiAutoScaledAxis opts ps = autoScaledAxis opts (otherExtremities ++ ps)
-    otherExtremities = maybe [] (\(x, y) -> [x, y]) possibleOtherExtremities
+    semiAutoScaledAxis opts ps = autoScaledAxis opts (extremities ++ ps)
+    extremities = maybe [] (\(lo :*: hi) -> [lo, hi]) exs
 
     info = plot_lines_values ^= [zip (fromU (fromPoints points)) (fromU spdf)]
          $ defaultPlotLines
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.