Skip to content

Commit f8f747b

Browse files
LysxiaBodigrim
authored andcommitted
Use bcompareWithin to automate tests for linear time complexity
1 parent 6a90672 commit f8f747b

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

benchmarks/haskell/Benchmarks/Micro.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@ module Benchmarks.Micro (benchmark) where
44

55
import qualified Data.Text.Lazy as TL
66
import qualified Data.Text as T
7-
import Test.Tasty.Bench (Benchmark, bgroup, bench, nf)
7+
import Test.Tasty.Bench (Benchmark, Benchmarkable, bgroup, bcompareWithin, bench, nf)
88

99
benchmark :: Benchmark
1010
benchmark = bgroup "Micro"
11-
[ -- Accessing i-th element should take O(i) time.
12-
-- The 2k case should run in 2x the time of the 1k case.
13-
bgroup "Lazy.inits"
14-
[ bench "last 1k" $ nf (last . TL.inits) (chunks 1000)
15-
, bench "last 2k" $ nf (last . TL.inits) (chunks 2000)
16-
, bench "map-take1 1k" $ nf (map (TL.take 1) . TL.inits) (chunks 1000)
17-
, bench "map-take1 2k" $ nf (map (TL.take 1) . TL.inits) (chunks 2000)
18-
]
11+
[ blinear "lazy-inits--last" 500000 2 0.1 $ \len ->
12+
nf (last . TL.inits) (chunks len)
13+
, blinear "lazy-inits--map-take1" 500000 2 0.1 $ \len ->
14+
nf (map (TL.take 1) . TL.inits) (chunks len)
1915
]
2016

2117
chunks :: Int -> TL.Text
2218
chunks n = TL.fromChunks (replicate n (T.pack "a"))
19+
20+
-- Check that running an action with input length (m * baseLen)
21+
-- runs m times slower than the same action with input length baseLen.
22+
blinear :: String -- ^ Name (must be globally unique!)
23+
-> Int -- ^ Base length
24+
-> Int -- ^ Multiplier m
25+
-> Double -- ^ Slack s
26+
-> (Int -> Benchmarkable) -- ^ Action to measure, parameterized by input length
27+
-> Benchmark
28+
blinear name baseLen m s run = bgroup name
29+
[ bench "baseline" $ run baseLen
30+
, bcompareWithin (fromIntegral m * (1 - s)) (fromIntegral m * (1 + s)) (name ++ ".baseline") $
31+
bench ("x" ++ show m) $ run (m * baseLen)
32+
]

0 commit comments

Comments
 (0)