Skip to content

Commit

Permalink
Merge branch 'florius0-fix/benchmarks'
Browse files Browse the repository at this point in the history
  • Loading branch information
QuinnWilton committed Feb 3, 2022
2 parents 4ddd4c8 + e716d13 commit 6c61c3e
Show file tree
Hide file tree
Showing 61 changed files with 1,263 additions and 999 deletions.
9 changes: 5 additions & 4 deletions bench/witchcraft/applicative/function_bench.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Witchcraft.Applicative.FunctionBench do
@moduledoc false

use Benchfella
use Witchcraft.Applicative

Expand All @@ -10,7 +12,7 @@ defmodule Witchcraft.Applicative.FunctionBench do
# Data Types #
# ---------- #

def fun(x), do: "#{inspect x}-#{inspect x}"
def fun(x), do: "#{inspect(x)}-#{inspect(x)}"

# -------------- #
# Test Functions #
Expand All @@ -23,7 +25,6 @@ defmodule Witchcraft.Applicative.FunctionBench do
# Applicative #
###############

bench "of/1", do: to_fun(42)
bench "of/2", do: of(&fun/1, &twice/1)

bench("of/1", do: to_fun(42))
bench("of/2", do: of(&fun/1, &twice/1))
end
7 changes: 4 additions & 3 deletions bench/witchcraft/applicative/list_bench.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Witchcraft.Applicative.ListBench do
@moduledoc false

use Benchfella
use Witchcraft.Applicative

Expand All @@ -23,7 +25,6 @@ defmodule Witchcraft.Applicative.ListBench do
# Applicative #
###############

bench "of/1", do: to_list(@to_wrap)
bench "of/2", do: of(@list, @to_wrap)

bench("of/1", do: to_list(@to_wrap))
bench("of/2", do: of(@list, @to_wrap))
end
7 changes: 4 additions & 3 deletions bench/witchcraft/applicative/tuple_bench.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Witchcraft.Applicative.TupleBench do
@moduledoc false

use Benchfella
use Witchcraft.Applicative

Expand All @@ -23,7 +25,6 @@ defmodule Witchcraft.Applicative.TupleBench do
# Applicative #
###############

bench "of/1", do: to_tuple(@to_wrap)
bench "of/2", do: of(@tuple, @to_wrap)

bench("of/1", do: to_tuple(@to_wrap))
bench("of/2", do: of(@tuple, @to_wrap))
end
141 changes: 97 additions & 44 deletions bench/witchcraft/apply/function_bench.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule Witchcraft.Apply.FunBench do
@moduledoc false

import Kernel, except: [then: 2]

use Benchfella
use Witchcraft.Apply

Expand All @@ -15,7 +19,8 @@ defmodule Witchcraft.Apply.FunBench do

defp slow_fun(z) do
Process.sleep(20)
fn(x, y) ->

fn x, y ->
x + y * z
end
end
Expand All @@ -33,56 +38,95 @@ defmodule Witchcraft.Apply.FunBench do
# Data #
# ==== #

bench "data convey/2", do: fn x -> fun(x) end |> convey(fn x -> fun(x) end)
bench "data ap/2", do: fn x -> fun(x) end |> ap(fn x -> fun(x) end)
bench("data convey/2", do: fn x -> fun(x) end |> convey(fn x -> fun(x) end))
bench("data ap/2", do: fn x -> fun(x) end |> ap(fn x -> fun(x) end))

bench("data <<~/2", do: fn x -> fun(x) end <<~ fn x -> fun(x) end)
bench("data <<~/2", do: fn x -> fun(x) end <<~ fn x -> fun(x) end)

bench("data ~>>/2", do: fn x -> fun(x) end ~>> fn x -> fun(x) end)
bench("data ~>>/2", do: fn x -> fun(x) end ~>> fn x -> fun(x) end)

bench("data provide/2", do: fn x -> fun(x) end |> provide(fn x -> fun(x) end))
bench("data supply/2", do: fn x -> fun(x) end |> supply(fn x -> fun(x) end))

bench("data lift/3", do: lift(fn x -> fun(x) end, fn x -> fun(x) end, &+/2))

bench "data <<~/2", do: fn x -> fun(x) end <<~ fn x -> fun(x) end
bench "data <<~/2", do: fn x -> fun(x) end <<~ fn x -> fun(x) end
bench("data lift/4",
do: lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x, y, z -> x + y + z end)
)

bench "data ~>>/2", do: fn x -> fun(x) end ~>> fn x -> fun(x) end
bench "data ~>>/2", do: fn x -> fun(x) end ~>> fn x -> fun(x) end
bench("data lift/5",
do:
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn w, x, y, z ->
w + x + y + z
end)
)

bench "data provide/2", do: fn x -> fun(x) end |> provide(fn x -> fun(x) end)
bench "data supply/2", do: fn x -> fun(x) end |> supply(fn x -> fun(x) end)
bench("data over/3", do: over(&+/2, fn x -> fun(x) end, fn x -> fun(x) end))

bench "data lift/3", do: lift(fn x -> fun(x) end, fn x -> fun(x) end, &+/2)
bench "data lift/4", do: lift(fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y, z) -> x + y + z end)
bench "data lift/5", do: lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(w, x, y, z) -> w + x + y + z end)
bench("data over/4",
do: over(fn x, y, z -> x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end)
)

bench "data over/3", do: over(&+/2, fn x -> fun(x) end, fn x -> fun(x) end)
bench "data over/4", do: over(fn(x, y, z) -> x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end)
bench "data over/5", do: over(fn(w, x, y, z) -> w + x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end)
bench("data over/5",
do:
over(fn w, x, y, z -> w + x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end, fn x ->
fun(x)
end)
)

bench "following/2", do: fn x -> fun(x) end |> following(fn x -> fun(x) end)
bench "then/2", do: fn x -> fun(x) end |> then(fn x -> fun(x) end)
bench("following/2", do: fn x -> fun(x) end |> following(fn x -> fun(x) end))
bench("then/2", do: fn x -> fun(x) end |> then(fn x -> fun(x) end))

# ----- #
# Async #
# ----- #

bench "data async_convey/2", do: fn x -> fun(x) end |> async_convey(fn x -> fun(x) end)
bench "data async_ap/2", do: fn x -> fun(x) end |> async_ap(fn x -> fun(x) end)
bench("data async_convey/2", do: fn x -> fun(x) end |> async_convey(fn x -> fun(x) end))
bench("data async_ap/2", do: fn x -> fun(x) end |> async_ap(fn x -> fun(x) end))

bench "async_lift/3", do: async_lift(fn x -> fun(x) end, fn x -> fun(x) end, &+/2)
bench "async_lift/4", do: async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y, z) -> x + y + z end)
bench "async_lift/5", do: async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(w, x, y, z) -> w + x + y + z end)
bench("async_lift/3", do: async_lift(fn x -> fun(x) end, fn x -> fun(x) end, &+/2))

bench "async_over/3", do: async_over(&+/2, fn x -> fun(x) end, fn x -> fun(x) end)
bench "async_over/4", do: async_over(fn(x, y, z) -> x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end)
bench "async_over/5", do: async_over(fn(w, x, y, z) -> w + x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end)
bench("async_lift/4",
do: async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x, y, z -> x + y + z end)
)

bench "!!! convey/2", do: fn x -> fun(x) end |> convey(fn y -> slow_fun(y) end)
bench "!!! ap/2", do: fn y -> slow_fun(y) end |> ap(fn x -> fun(x) end)
bench("async_lift/5",
do:
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn w, x, y, z ->
w + x + y + z
end)
)

bench("async_over/3", do: async_over(&+/2, fn x -> fun(x) end, fn x -> fun(x) end))

bench("async_over/4",
do: async_over(fn x, y, z -> x + y + z end, fn x -> fun(x) end, fn x -> fun(x) end)
)

bench("async_over/5",
do:
async_over(
fn w, x, y, z -> w + x + y + z end,
fn x -> fun(x) end,
fn x -> fun(x) end,
fn x -> fun(x) end
)
)

bench("!!! convey/2", do: fn x -> fun(x) end |> convey(fn y -> slow_fun(y) end))
bench("!!! ap/2", do: fn y -> slow_fun(y) end |> ap(fn x -> fun(x) end))

bench "!!! lift/3" do
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y) ->
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x, y ->
Process.sleep(20)
x + y
end)
end

bench "!!! lift/4" do
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y, z) ->
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x, y, z ->
Process.sleep(20)
x + y + z
end)
Expand All @@ -91,22 +135,25 @@ defmodule Witchcraft.Apply.FunBench do
# also very slow, due to exponential complexity of multiple fun dimensions
# 50^4 = 6_250_000 items to process
bench "!!! lift/5" do
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(w, x, y, z) ->
lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn w,
x,
y,
z ->
Process.sleep(20)
w + x + y + z
end)
end

bench "!!! over/3" do
fn(x, y) ->
fn x, y ->
Process.sleep(20)
x + y
end
|> over(fn x -> fun(x) end, fn x -> fun(x) end)
end

bench "!!! over/4" do
fn(x, y, z) ->
fn x, y, z ->
Process.sleep(20)
x + y + z
end
Expand All @@ -115,55 +162,61 @@ defmodule Witchcraft.Apply.FunBench do

# So slow
bench "!!! over/5" do
fn(w, x, y, z) ->
fn w, x, y, z ->
Process.sleep(20)
w + x + y + z
end
|> over(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end)
end

bench "!!! async_convey/2", do: fn x -> fun(x) end |> async_convey(fn y -> slow_fun(y) end)
bench "!!! async_ap/2", do: fn y -> slow_fun(y) end |> async_ap(fn x -> fun(x) end)
bench("!!! async_convey/2", do: fn x -> fun(x) end |> async_convey(fn y -> slow_fun(y) end))
bench("!!! async_ap/2", do: fn y -> slow_fun(y) end |> async_ap(fn x -> fun(x) end))

bench "!!! async_lift/3" do
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y) ->
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x, y ->
Process.sleep(20)
x + y
end)
end

bench "!!! async_lift/4" do
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(x, y, z) ->
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x, y, z ->
Process.sleep(20)
x + y + z
end)
end

bench "!!! async_lift/5" do
async_lift(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end, fn(w, x, y, z) ->
Process.sleep(20)
w + x + y + z
end)
async_lift(
fn x -> fun(x) end,
fn x -> fun(x) end,
fn x -> fun(x) end,
fn x -> fun(x) end,
fn w, x, y, z ->
Process.sleep(20)
w + x + y + z
end
)
end

bench "!!! async_over/3" do
fn(x, y) ->
fn x, y ->
Process.sleep(20)
x + y
end
|> async_over(fn x -> fun(x) end, fn x -> fun(x) end)
end

bench "!!! async_over/4" do
fn(x, y, z) ->
fn x, y, z ->
Process.sleep(20)
x + y + z
end
|> async_over(fn x -> fun(x) end, fn x -> fun(x) end, fn x -> fun(x) end)
end

bench "!!! async_over/5" do
fn(w, x, y, z) ->
fn w, x, y, z ->
Process.sleep(20)
w + x + y + z
end
Expand Down
Loading

0 comments on commit 6c61c3e

Please sign in to comment.