Skip to content

Instantly share code, notes, and snippets.

@dkuku
Created July 11, 2023 19:04
Show Gist options
  • Save dkuku/9004bbf92cb3b81e8ed6fe211b9c7fc6 to your computer and use it in GitHub Desktop.
Save dkuku/9004bbf92cb3b81e8ed6fe211b9c7fc6 to your computer and use it in GitHub Desktop.
compare between half byte matching and full byte
# Untitled notebook
```elixir
Mix.install([:benchee])
```
<!-- livebook:{"output":true} -->
```
:ok
```
## Section
```elixir
defmodule ParseInt do
def parse_new(<<3::4, x0::4, 3::4, x1::4, 3::4, x2::4, 3::4, x3::4>>)
when x0 < 10 and x1 < 10 and x2 < 10 and x3 < 10 do
x3 + x2 * 10 + x1 * 100 + x0 * 1000
end
def nimble(<<x0, x1, x2, x3>>)
when x0 >= 48 and x0 <= 57 and (x1 >= 48 and x1 <= 57) and
(x2 >= 48 and x2 <= 57) and (x3 >= 48 and x3 <= 57) do
(x3 - 48) * 1 + (x2 - 48) * 10 + (x1 - 48) * 100 + (x0 - 48) * 1000
end
end
{ParseInt.parse_new("1123"), ParseInt.nimble("1123")}
```
<!-- livebook:{"output":true} -->
```
{1123, 1123}
```
```elixir
list = Enum.map(1000..9999, &to_string(&1))
Benchee.run(
%{
"original" => fn -> Enum.map(list, &ParseInt.nimble(&1)) end,
"half byte" => fn -> Enum.map(list, &ParseInt.parse_new(&1)) end
},
time: 10,
memory_time: 2
)
```
<!-- livebook:{"output":true} -->
```
Operating System: Linux
CPU Information: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.41 GB
Elixir 1.15.2
Erlang 26.0.2
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 2 s
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 28 s
Benchmarking half byte ...
Benchmarking original ...
Name ips average deviation median 99th %
half byte 3.04 K 328.56 μs ±31.44% 277.56 μs 643.98 μs
original 2.93 K 341.79 μs ±34.93% 288.44 μs 672.01 μs
Comparison:
half byte 3.04 K
original 2.93 K - 1.04x slower +13.23 μs
Memory usage statistics:
Name Memory usage
half byte 493.70 KB
original 493.70 KB - 1.00x memory usage +0 KB
**All measurements for memory usage were the same**
```
<!-- livebook:{"output":true} -->
```
%Benchee.Suite{
system: %{
erlang: "26.0.2",
os: :Linux,
elixir: "1.15.2",
num_cores: 4,
cpu_speed: "Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz",
available_memory: "7.41 GB"
},
configuration: %Benchee.Configuration{
parallel: 1,
time: 10000000000.0,
warmup: 2000000000.0,
memory_time: 2000000000.0,
reduction_time: 0.0,
pre_check: false,
formatters: [Benchee.Formatters.Console],
percentiles: ~c"2c",
print: %{configuration: true, fast_warning: true, benchmarking: 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: "half byte",
job_name: "half byte",
function: #Function<43.125776118/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: 328561.142701309,
ips: 3043.5735393977734,
std_dev: 103284.5851447088,
std_dev_ratio: 0.3143542303741121,
std_dev_ips: 956.7602175643993,
median: 277555.0,
percentiles: %{50 => 277555.0, 99 => 643976.7300000001},
mode: 268224,
minimum: 236637,
maximum: 2314432,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 30252
},
samples: [688263, 771050, 271289, 274052, 260751, 258702, 259726, 265226, 261569, 261239,
260193, 269702, 260571, 261499, 260747, 269895, 259177, 257781, 280745, 258812, 260203,
257838, 263829, 259717, 259018, 259325, 262628, 260705, 259648, 261206, 320887, 456552,
491386, ...]
},
memory_usage_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 505552.0,
ips: nil,
std_dev: 0.0,
std_dev_ratio: 0.0,
std_dev_ips: nil,
median: 505552.0,
percentiles: %{50 => 505552.0, 99 => 505552.0},
mode: 505552,
minimum: 505552,
maximum: 505552,
relative_more: nil,
relative_less: nil,
absolute_difference: nil,
sample_size: 762
},
samples: [505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552,
505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552,
505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552,
...]
},
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: "original",
job_name: "original",
function: #Function<43.125776118/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: 341794.68619764806,
ips: 2925.7330215535726,
std_dev: 119395.29480834414,
std_dev_ratio: 0.3493187566388963,
std_dev_ips: 1022.0134213464551,
median: 288438.5,
percentiles: %{50 => 288438.5, 99 => 672008.4499999995},
mode: [286984, 279526],
minimum: 229271,
maximum: 8252562,
relative_more: 1.040277262817927,
relative_less: 0.9612821848005954,
absolute_difference: 13233.543496339058,
sample_size: 29082
},
samples: [465898, 719290, 415537, 373134, 270124, 266887, 272347, 267605, 267224, 269328,
275135, 269780, 270583, 283158, 344546, 282544, 344435, 535706, 562648, 537594, 546874,
296727, 281043, 287341, 273003, 271726, 456945, 479387, 534290, 521286, 494966, 323894,
...]
},
memory_usage_data: %Benchee.CollectionData{
statistics: %Benchee.Statistics{
average: 505552.0,
ips: nil,
std_dev: 0.0,
std_dev_ratio: 0.0,
std_dev_ips: nil,
median: 505552.0,
percentiles: %{50 => 505552.0, 99 => 505552.0},
mode: 505552,
minimum: 505552,
maximum: 505552,
relative_more: 1.0,
relative_less: 1.0,
absolute_difference: 0.0,
sample_size: 754
},
samples: [505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552,
505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552,
505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, 505552, ...]
},
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