Benchmarks
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])
Navigation
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 with1000
elements -
Create a
medium_map
variable with10_000
elements -
Create a
large_map
variable with100_000
elements -
Create a
small_keyword_list
variable with1000
elements -
Create a
medium_keyword_list
variable with10_000
elements -
Create a
large_keyword_list
variable with100_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.
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish benchmarks exercise"