-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetMap.elm
64 lines (49 loc) · 1.76 KB
/
SetMap.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module ImprovingPerformance.ElmCore.SetMap exposing (main)
{-| Changing `Set.map` to avoid an intermediate list representation.
Results: Quite a bit faster. Note that when all benchmarks are run together, the results somewhat seem worse,
that's why I've saved the results of individual benchmark runs. To be tested further.
-}
import Benchmark exposing (Benchmark, describe)
import Benchmark.Runner exposing (BenchmarkProgram, program)
import Set exposing (Set)
{-| Original version:
map : (comparable -> comparable2) -> Set comparable -> Set comparable2
map func set =
fromList (foldl (\x xs -> func x :: xs) [] set)
-}
altSetMap : (comparable -> comparable2) -> Set comparable -> Set comparable2
altSetMap func set =
Set.foldl (\x acc -> Set.insert (func x) acc) Set.empty set
suite : Benchmark
suite =
let
tenElements : Set Int
tenElements =
List.range 1 10 |> Set.fromList
thousandElements : Set Int
thousandElements =
List.range 1 1000 |> Set.fromList
in
describe "Set.map"
[ Benchmark.compare "Empty dicts"
"Original"
(\() -> Set.map increment Set.empty)
"Using Set.insert"
(\() -> altSetMap increment Set.empty)
, Benchmark.compare "10 elements"
"Original"
(\() -> Set.map increment tenElements)
"Using Set.insert"
(\() -> altSetMap increment tenElements)
, Benchmark.compare "1000 elements"
"Original"
(\() -> Set.map increment thousandElements)
"Using Set.insert"
(\() -> altSetMap increment thousandElements)
]
main : BenchmarkProgram
main =
program suite
increment : number -> number
increment a =
a + 1