pattern matching vs. indexedMap
name | runs / second | % change | goodness of fit |
---|---|---|---|
Pattern Match | 12,168 | - | 99.74% |
indexedMap | 12,284 | +0.95% | 99.86% |
module BenchmarkPattern exposing (main) | |
import Benchmark exposing (..) | |
import Benchmark.Runner exposing (BenchmarkProgram, program) | |
main : BenchmarkProgram | |
main = | |
program suite | |
suite : Benchmark | |
suite = | |
let | |
sampleList : List Int | |
sampleList = | |
List.range 0 100 | |
|> List.map (\item -> ( item, List.range 0 10 )) | |
addOne : Int -> Int | |
addOne = | |
(+) 1 | |
addOneExceptForFirstPatternMatch : List Int -> List Int | |
addOneExceptForFirstPatternMatch list = | |
case list of | |
[] -> | |
[] | |
head :: tail -> | |
head :: List.map addOne tail | |
patternMatch : List ( Int, List Int ) -> List ( Int, List Int ) | |
patternMatch list = | |
case list of | |
[] -> | |
[] | |
head :: tail -> | |
Tuple.mapSecond addOneExceptForFirstPatternMatch head :: List.map (Tuple.mapSecond (List.map addOne)) tail | |
addOneExceptForFirstIndexedMap : List Int -> List Int | |
addOneExceptForFirstIndexedMap = | |
List.indexedMap | |
(\i item -> | |
if i == 0 then | |
item | |
else | |
addOne item | |
) | |
indexedMap : List ( Int, List Int ) -> List ( Int, List Int ) | |
indexedMap = | |
List.indexedMap | |
(\i tuple -> | |
if i == 0 then | |
Tuple.mapSecond addOneExceptForFirstIndexedMap tuple | |
else | |
Tuple.mapSecond (List.map addOne) tuple | |
) | |
in | |
describe "" | |
[ -- nest as many descriptions as you like | |
Benchmark.compare "Pattern matching vs. indexedMap" | |
"Pattern Match" | |
(\_ -> patternMatch sampleList) | |
"indexedMap" | |
(\_ -> indexedMap sampleList) | |
] |