Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Created November 16, 2022 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janiczek/a2bc7a3b1b46670ae7b956f084d03f68 to your computer and use it in GitHub Desktop.
Save Janiczek/a2bc7a3b1b46670ae7b956f084d03f68 to your computer and use it in GitHub Desktop.
basics-extra orderBy benchmark
{
"type": "application",
"source-directories": [
".",
"../src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm-explorations/benchmark": "1.0.2"
},
"indirect": {
"BrianHicks/elm-trend": "2.1.3",
"elm/json": "1.1.3",
"elm/regex": "1.0.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3",
"mdgriffith/style-elements": "5.0.2",
"robinheghan/murmur3": "1.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
module OrderBench exposing (main)
{-| To run the benchmarks, first compile them with
elm make OrderBench.elm --optimize
from this folder. Then open the generated index.html file in a browser.
-}
import Array exposing (Array)
import Basics.Extra
import Benchmark exposing (..)
import Benchmark.Runner exposing (BenchmarkProgram, program)
import Dict exposing (Dict)
import Set exposing (Set)
main : BenchmarkProgram
main =
program suite
fold : List (a -> a -> Order) -> (a -> a -> Order)
fold comparators =
\a b ->
let
step : (a -> a -> Order) -> Order -> Order
step comparator acc =
case acc of
EQ ->
comparator a b
_ ->
acc
in
List.foldl step EQ comparators
recursive : List (a -> a -> Order) -> (a -> a -> Order)
recursive comparators =
\a b ->
case comparators of
[] ->
EQ
comparator :: rest ->
case comparator a b of
EQ ->
recursive rest a b
other ->
other
suite : Benchmark
suite =
let
comparators =
[ .color >> colorToComparable |> Basics.Extra.toOrder
, Basics.Extra.toOrder .cylinders
, Basics.Extra.toOrder .manufacturer
, Basics.Extra.toOrder .model
]
all =
[ dodgeViper, fordMustangEco, bmw340i, fordMustangShelby ]
in
Benchmark.compare "orderBy"
"fold"
(\_ -> List.sortWith (fold comparators) all)
"recursive"
(\_ -> List.sortWith (recursive comparators) all)
type Color
= Red
| Black
| Blue
colorToComparable : Color -> Int
colorToComparable =
\c ->
case c of
Red ->
0
Black ->
1
Blue ->
2
type alias Car =
{ manufacturer : String
, model : String
, cylinders : Int
, color : Color
}
fordMustangEco : Car
fordMustangEco =
{ manufacturer = "Ford"
, model = "Mustang EcoBoost"
, cylinders = 4
, color = Blue
}
fordMustangShelby : Car
fordMustangShelby =
{ manufacturer = "Ford"
, model = "Mustang Shelby GT350"
, cylinders = 8
, color = Red
}
dodgeViper : Car
dodgeViper =
{ manufacturer = "Dodge"
, model = "Viper ACR"
, cylinders = 10
, color = Black
}
bmw340i : Car
bmw340i =
{ manufacturer = "BMW"
, model = "340i"
, cylinders = 6
, color = Blue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment