Skip to content

Commit

Permalink
Use bcompareWithin to automate tests for linear time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia authored and Bodigrim committed Mar 21, 2024
1 parent 6a90672 commit f8f747b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions benchmarks/haskell/Benchmarks/Micro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ module Benchmarks.Micro (benchmark) where

import qualified Data.Text.Lazy as TL
import qualified Data.Text as T
import Test.Tasty.Bench (Benchmark, bgroup, bench, nf)
import Test.Tasty.Bench (Benchmark, Benchmarkable, bgroup, bcompareWithin, bench, nf)

benchmark :: Benchmark
benchmark = bgroup "Micro"
[ -- Accessing i-th element should take O(i) time.
-- The 2k case should run in 2x the time of the 1k case.
bgroup "Lazy.inits"
[ bench "last 1k" $ nf (last . TL.inits) (chunks 1000)
, bench "last 2k" $ nf (last . TL.inits) (chunks 2000)
, bench "map-take1 1k" $ nf (map (TL.take 1) . TL.inits) (chunks 1000)
, bench "map-take1 2k" $ nf (map (TL.take 1) . TL.inits) (chunks 2000)
]
[ blinear "lazy-inits--last" 500000 2 0.1 $ \len ->
nf (last . TL.inits) (chunks len)
, blinear "lazy-inits--map-take1" 500000 2 0.1 $ \len ->
nf (map (TL.take 1) . TL.inits) (chunks len)
]

chunks :: Int -> TL.Text
chunks n = TL.fromChunks (replicate n (T.pack "a"))

-- Check that running an action with input length (m * baseLen)
-- runs m times slower than the same action with input length baseLen.
blinear :: String -- ^ Name (must be globally unique!)
-> Int -- ^ Base length
-> Int -- ^ Multiplier m
-> Double -- ^ Slack s
-> (Int -> Benchmarkable) -- ^ Action to measure, parameterized by input length
-> Benchmark
blinear name baseLen m s run = bgroup name
[ bench "baseline" $ run baseLen
, bcompareWithin (fromIntegral m * (1 - s)) (fromIntegral m * (1 + s)) (name ++ ".baseline") $
bench ("x" ++ show m) $ run (m * baseLen)
]

0 comments on commit f8f747b

Please sign in to comment.