Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Benchmarks

deprecated_benchmarks.livemd

Benchmarks

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:benchee, "~> 1.1"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Overview

In this set of exercises, you’re going to use Benchee to benchmark common operations.

You can also provide multiple inputs with Benchee like so:

Benchee.run(
    %{
    "length of list": fn input -> length(input) end
},
inputs: %{
    "Small": Enum.to_list(1..1000),
    "Medium": Enum.to_list(1..10_000),
    "Large": Enum.to_list(1..100_000),
}
)

If you want to use different input for each function, you could separate the input into some data structure.

large_list = Enum.to_list(1..1000)
large_tuple = List.to_tuple(large_list)

Benchee.run(
    %{
    "length of list": fn {list, _tuple} -> length(list) end,
    "length of tuple": fn {_list, tuple} -> tuple_size(tuple) end
},
inputs: %{
    "Large": {large_list, large_tuple},
}
)

Lists Vs Tuples

Access

In the Elixir cell below, benchmark the following:

  • Determine the length of a list
  • Determine the length of a tuple
  • Retrieve the first element in a list
  • Retrieve the last element in a list
  • Retrieve the first element in a tuple
  • Retrieve the last element in a tuple

Ensure that you test these operations with 1000 elements, 10_000 elements, and 100_000 elements.

Keyword Lists Vs Maps

In the Elixir cell below,

  • Create a small_map variable with 1000 elements
  • Create a medium_map variable with 10_000 elements
  • Create a large_map variable with 100_000 elements
  • Create a small_keyword_list variable with 1000 elements
  • Create a medium_keyword_list variable with 10_000 elements
  • Create a large_keyword_list variable with 100_000 elements

In the Elixir cell below, benchmark the following:

  • Update an element in the map.
  • Update the first element in the keyword list.
  • Update the last element in the keyword list

In the Elixir cell below, benchmark the following:

  • Insert a new element into a map.
  • Insert a new element the start of a keyword list.
  • Insert a new element the end of a keyword list.

Ensure that you test these operations with 1000 elements, 10_000 elements, and 100_000 elements.

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, file_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(progress_path, Jason.encode!(Map.put(existing_progress, file_name, completed)))
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout solutions
$ git checkout -b benchmarks-exercise
$ git add .
$ git commit -m "finish benchmarks exercise"
$ git push origin benchmarks-exercise

Create a pull request from your benchmarks-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your instructor by including @BrooklinJazz in your PR description to get feedback. You (or your instructor) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.