@@ -4,19 +4,29 @@ module Benchmarks.Micro (benchmark) where
4
4
5
5
import qualified Data.Text.Lazy as TL
6
6
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 )
8
8
9
9
benchmark :: Benchmark
10
10
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)
19
15
]
20
16
21
17
chunks :: Int -> TL. Text
22
18
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