Skip to content

Commit

Permalink
Commits benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
gampleman committed Mar 21, 2024
1 parent e7220f8 commit a515049
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 9 deletions.
44 changes: 35 additions & 9 deletions benchmarks/src/Benchmarks.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Benchmark.Runner.Alternative as BenchmarkRunner
import List.Extra
import List.Extra.Unfoldr
import List.Extra.UniquePairs
import List.Extra.GroupsOf
import Set exposing (Set)
import Set.Extra.AreDisjoint
import Set.Extra.SymmetricDifference
Expand All @@ -29,13 +30,15 @@ import String.Extra.IsBlank
main : BenchmarkRunner.Program
main =
describe "for core-extra"
[ application
, array
, arrayExtra
, listExtra
, tupleExtra
, setExtra
, stringExtra
[
-- application
-- , array
-- , arrayExtra
-- ,
listExtra
-- , tupleExtra
-- , setExtra
-- , stringExtra
]
|> BenchmarkRunner.program

Expand Down Expand Up @@ -182,17 +185,40 @@ listExtra =
List.range 1 100
in
describe "List.Extra"
[ rank "uniquePairs"
([ rank "uniquePairs"
(\uniquePairs -> uniquePairs intList)
[ ( "original (++)", List.Extra.UniquePairs.originalConcat )
, ( "tail-recursive", List.Extra.UniquePairs.tailRecursive )
]
, rank "unfoldr"
, rank "unfoldr"
(\unfoldr -> unfoldr subtractOneUntilZero 100)
[ ( "original", List.Extra.Unfoldr.nonTailRecursive )
, ( "tail-recursive", List.Extra.Unfoldr.tailRecursive )
]
]
++ List.concatMap toComparisonsGroupsOfWithStep (List.range 1 4)
)

toComparisonsGroupsOfWithStep : Int -> List Benchmark
toComparisonsGroupsOfWithStep exponent =
let
listSize =
10 ^ exponent

range =
List.range 1 listSize
in
[ rank ("groupsOfWithStep 3 2 [1.." ++ String.fromInt listSize ++ "]")
(\impl -> impl 3 2 range)
[ ( "using elm-core's List.tail", List.Extra.GroupsOf.coreTailGroupsOfWithStep )
, ( "using fully tail-recursive List.tail", List.Extra.GroupsOf.tailRecGroupsOfWithStep )
]
, rank ("greedyGroupsOfWithStep 3 2 [1.." ++ String.fromInt listSize ++ "]")
(\impl -> impl 3 2 range)
[ ( "using elm-core's List.tail", List.Extra.GroupsOf.coreTailGreedyGroupsOfWithStep )
, ( "using fully tail-recursive List.tail", List.Extra.GroupsOf.tailRecGreedyGroupsOfWithStep )
]
]


tupleExtra : Benchmark
Expand Down
123 changes: 123 additions & 0 deletions benchmarks/src/List/Extra/GroupsOf.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
module List.Extra.GroupsOf exposing (coreTailGroupsOfWithStep, coreTailGreedyGroupsOfWithStep, tailRecGroupsOfWithStep, tailRecGreedyGroupsOfWithStep)

import Benchmark
import Benchmark.Runner.Alternative as BenchmarkRunner


coreTailGroupsOfWithStep : Int -> Int -> List a -> List (List a)
coreTailGroupsOfWithStep size step list =
if size <= 0 || step <= 0 then
[]

else
let
go : List a -> List (List a) -> List (List a)
go xs acc =
if List.isEmpty xs then
List.reverse acc

else
let
thisGroup =
List.take size xs
in
if size == List.length thisGroup then
let
rest =
List.drop step xs
in
go rest (thisGroup :: acc)

else
List.reverse acc
in
go list []


coreTailGreedyGroupsOfWithStep : Int -> Int -> List a -> List (List a)
coreTailGreedyGroupsOfWithStep size step list =
if size <= 0 || step <= 0 then
[]

else
let
go : List a -> List (List a) -> List (List a)
go xs acc =
if List.isEmpty xs then
List.reverse acc

else
go
(List.drop step xs)
(List.take size xs :: acc)
in
go list []


tailRecGroupsOfWithStep : Int -> Int -> List a -> List (List a)
tailRecGroupsOfWithStep size step list =
if size <= 0 || step <= 0 then
[]

else
let
go : List a -> List (List a) -> List (List a)
go xs acc =
if List.isEmpty xs then
List.reverse acc

else
let
thisGroup =
takeTailRec size xs
in
if size == List.length thisGroup then
let
rest =
List.drop step xs
in
go rest (thisGroup :: acc)

else
List.reverse acc
in
go list []


tailRecGreedyGroupsOfWithStep : Int -> Int -> List a -> List (List a)
tailRecGreedyGroupsOfWithStep size step list =
if size <= 0 || step <= 0 then
[]

else
let
go : List a -> List (List a) -> List (List a)
go xs acc =
if List.isEmpty xs then
List.reverse acc

else
go
(List.drop step xs)
(takeTailRec size xs :: acc)
in
go list []


takeTailRec : Int -> List a -> List a
takeTailRec n list =
List.reverse (takeReverse n list [])


takeReverse : Int -> List a -> List a -> List a
takeReverse n list kept =
if n <= 0 then
kept

else
case list of
[] ->
kept

x :: xs ->
takeReverse (n - 1) xs (x :: kept)

0 comments on commit a515049

Please sign in to comment.