Skip to content

Instantly share code, notes, and snippets.

@seanmor5
Created June 23, 2023 22:16
Show Gist options
  • Save seanmor5/f87336365356d32922aa5d9b4ac24b51 to your computer and use it in GitHub Desktop.
Save seanmor5/f87336365356d32922aa5d9b4ac24b51 to your computer and use it in GitHub Desktop.
<!-- livebook:{"persist_outputs":true} -->
# zip create benchmark
```elixir
Mix.install([
{:benchee, "~> 1.0"}
])
```
<!-- livebook:{"output":true} -->
```
:ok
```
## Benchmark
```elixir
paths = Path.wildcard("axon/**/*") |> Enum.reject(&File.dir?/1)
```
<!-- livebook:{"output":true} -->
```
["axon/CHANGELOG.md", "axon/LICENSE", "axon/README.md", "axon/axon.png",
"axon/checkpoint/checkpoint_0_1.ckpt", "axon/checkpoint/checkpoint_1_1.ckpt",
"axon/checkpoint/checkpoint_2_1.ckpt", "axon/cover/Elixir.Axon.Activations.html",
"axon/cover/Elixir.Axon.Case.html", "axon/cover/Elixir.Axon.CompileError.html",
"axon/cover/Elixir.Axon.Compiler.html", "axon/cover/Elixir.Axon.Defn.html",
"axon/cover/Elixir.Axon.Display.html", "axon/cover/Elixir.Axon.Initializers.html",
"axon/cover/Elixir.Axon.Layers.html", "axon/cover/Elixir.Axon.Loop.State.html",
"axon/cover/Elixir.Axon.Loop.html", "axon/cover/Elixir.Axon.LossScale.html",
"axon/cover/Elixir.Axon.Losses.html", "axon/cover/Elixir.Axon.Metrics.html",
"axon/cover/Elixir.Axon.MixedPrecision.Policy.html", "axon/cover/Elixir.Axon.MixedPrecision.html",
"axon/cover/Elixir.Axon.Node.html", "axon/cover/Elixir.Axon.None.html",
"axon/cover/Elixir.Axon.Optimizers.html", "axon/cover/Elixir.Axon.Parameter.html",
"axon/cover/Elixir.Axon.Recurrent.html", "axon/cover/Elixir.Axon.Schedules.html",
"axon/cover/Elixir.Axon.Shape.html", "axon/cover/Elixir.Axon.Shared.html",
"axon/cover/Elixir.Axon.StatefulOutput.html", "axon/cover/Elixir.Axon.Updates.html",
"axon/cover/Elixir.Axon.html", "axon/cover/Elixir.AxonTestUtil.html",
"axon/cover/Elixir.Inspect.Axon.Loop.html",
"axon/cover/Elixir.Inspect.Axon.MixedPrecision.Policy.html",
"axon/cover/Elixir.Inspect.Axon.None.html", "axon/cover/Elixir.Inspect.Axon.html",
"axon/cover/Elixir.LazyOnly.html", "axon/cover/Elixir.LazyWrapped.html",
"axon/cover/Elixir.Nx.Container.Axon.None.html",
"axon/cover/Elixir.Nx.Container.Axon.StatefulOutput.html",
"axon/cover/Elixir.Nx.Container.LazyWrapped.html",
"axon/cover/Elixir.Nx.LazyContainer.LazyOnly.html", "axon/doc/404.html",
"axon/doc/Axon.Activations.html", "axon/doc/Axon.CompileError.html", "axon/doc/Axon.Display.html",
"axon/doc/Axon.Initializers.html", "axon/doc/Axon.Layers.html", ...]
```
```elixir
# option A
zip_just_paths = fn paths ->
:zip.create('test.zip', Enum.map(paths, &to_charlist/1), [{:cwd, '.'}])
end
# option B
zip_with_data = fn paths ->
:zip.create('test.zip', Enum.map(paths, &{to_charlist(&1), File.read!(&1)}), [{:cwd, '.'}])
end
```
<!-- livebook:{"output":true} -->
```
#Function<42.3316493/1 in :erl_eval.expr/6>
```
```elixir
Benchee.run(
%{
"zip just paths" => fn -> zip_just_paths.(paths) end,
"zip with open files" => fn -> zip_with_data.(paths) end
},
warmup: 15,
time: 60,
memory_time: 60
)
```
<!-- livebook:{"output":true} -->
```
Operating System: macOS
CPU Information: Apple M1 Max
Number of Available Cores: 10
Available memory: 32 GB
Elixir 1.14.2
Erlang 25.0.2
Benchmark suite executing with the following configuration:
warmup: 15 s
time: 1 min
memory time: 1 min
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 4.50 min
Benchmarking zip just paths ...
Benchmarking zip with open files ...
Name ips average deviation median 99th %
zip with open files 6.99 143.14 ms ±3.78% 141.61 ms 158.18 ms
zip just paths 6.32 158.10 ms ±1.47% 157.46 ms 168.62 ms
Comparison:
zip with open files 6.99
zip just paths 6.32 - 1.10x slower +14.96 ms
Memory usage statistics:
Name Memory usage
zip with open files 1.75 MB
zip just paths 2.27 MB - 1.30x memory usage +0.52 MB
**All measurements for memory usage were the same**
```
<!-- livebook:{"output":true} -->
```
%Benchee.Suite{
system: %{
available_memory: "32 GB",
cpu_speed: "Apple M1 Max",
elixir: "1.14.2",
erlang: "25.0.2",
num_cores: 10,
os: :macOS
},
configuration: %Benchee.Configuration{
parallel: 1,
time: 60000000000.0,
warmup: 15000000000.0,
memory_time: 60000000000.0,
reduction_time: 0.0,
pre_check: false,
formatters: [Benchee.Formatters.Console],
percentiles: '2c',
print: %{benchmarking: true, configuration: true, fast_warning: true},
inputs: nil,
save: false,
load: false,
unit_scaling: :best,
assigns: %{},
before_each: nil,
after_each: nil,
before_scenario: nil,
after_scenario: nil,
measure_function_call_overhead: false,
title: nil,
profile_after: false
},
scenarios: [
%Benchee.Scenario{
name: "zip with open files",
job_name: "zip with open files",
function: #Function<43.3316493/0 in :erl_eval.expr/6>,
input_name: :__no_input,
input: :__no_input,
before_each: nil,
after_each: nil,
before_scenario: nil,
after_scenario: nil,
tag: nil,
run_time_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 143142837.20714286,
ips: 6.986028917066203,
std_dev: 5408818.222535467,
std_dev_ratio: 0.03778616050978739,
std_dev_ips: 0.2639752099862797,
median: 141609271.5,
percentiles: %{50 => 141609271.5, 99 => 158178340.26000002},
mode: nil,
minimum: 136412396,
maximum: 165126040,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 420
},
samples: [139343777, 138528690, 137182793, 137000827, 141944584, 157753072, 138581482,
138313506, 136946806, 137058594, 141154529, 140532320, 142301086, 138761305, 138791022,
137137646, 138630919, 137505205, 136983499, 136849991, 138663989, 137349936, 138106234,
139044878, 138390724, 141215128, 141069083, 137578823, 136688972, 137081897, 137048702,
138768176, 140929264, ...]
},
memory_usage_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 1832256.0,
ips: nil,
std_dev: 0.0,
std_dev_ratio: 0.0,
std_dev_ips: nil,
median: 1832256.0,
percentiles: %{50 => 1832256.0, 99 => 1832256.0},
mode: 1832256,
minimum: 1832256,
maximum: 1832256,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 411
},
samples: [1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256,
1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256,
1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256, 1832256,
1832256, 1832256, 1832256, ...]
},
reductions_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: nil,
ips: nil,
std_dev: nil,
std_dev_ratio: nil,
std_dev_ips: nil,
median: nil,
percentiles: nil,
mode: nil,
minimum: nil,
maximum: nil,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 0
},
samples: []
}
},
%Benchee.Scenario{
name: "zip just paths",
job_name: "zip just paths",
function: #Function<43.3316493/0 in :erl_eval.expr/6>,
input_name: :__no_input,
input: :__no_input,
before_each: nil,
after_each: nil,
before_scenario: nil,
after_scenario: nil,
tag: nil,
run_time_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 158103359.99473685,
ips: 6.324976268899594,
std_dev: 2329743.495895422,
std_dev_ratio: 0.014735572324161722,
std_dev_ips: 0.09320214525897652,
median: 157462658.0,
percentiles: %{50 => 157462658.0, 99 => 168621184.54999998},
mode: nil,
minimum: 156270440,
maximum: 178966612,
relative_more: 1.1045146448085594,
relative_less: 0.9053750483981364,
absolute_difference: 14960522.78759399,
sample_size: 380
},
samples: [157426662, 158044685, 157565564, 156699039, 156984798, 157413167, 156954811,
157004915, 158341522, 158841218, 157595927, 156711117, 156542497, 157290946, 157021326,
156934444, 156932590, 157358127, 158599335, 156937859, 156999501, 160391032, 159206612,
156993773, 156975510, 156700309, 161154598, 157081947, 158012885, 156549849, 156719571,
156661512, ...]
},
memory_usage_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 2379784.0,
ips: nil,
std_dev: 0.0,
std_dev_ratio: 0.0,
std_dev_ips: nil,
median: 2379784.0,
percentiles: %{50 => 2379784.0, 99 => 2379784.0},
mode: 2379784,
minimum: 2379784,
maximum: 2379784,
relative_more: 1.298827238115198,
relative_less: 0.7699253377617464,
absolute_difference: 547528.0,
sample_size: 377
},
samples: [2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784,
2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784,
2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784, 2379784,
2379784, 2379784, ...]
},
reductions_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: nil,
ips: nil,
std_dev: nil,
std_dev_ratio: nil,
std_dev_ips: nil,
median: nil,
percentiles: nil,
mode: nil,
minimum: nil,
maximum: nil,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 0
},
samples: []
}
}
]
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment