Skip to content

Instantly share code, notes, and snippets.

@ryanwinchester
Forked from moogle19/bench.ex
Created October 3, 2022 22:52
Show Gist options
  • Save ryanwinchester/36661089713ac0c7f9a1d4e68306ac27 to your computer and use it in GitHub Desktop.
Save ryanwinchester/36661089713ac0c7f9a1d4e68306ac27 to your computer and use it in GitHub Desktop.
Masking benchmark
defmodule Bench do
def orig(payload, mask) do
maskstream = <<mask::32>> |> :binary.bin_to_list() |> Stream.cycle()
payload
|> :binary.bin_to_list()
|> Enum.zip(maskstream)
|> Enum.map(fn {x, y} -> Bitwise.bxor(x, y) end)
|> :binary.list_to_bin()
end
def mask(payload, mask, acc \\ <<>>)
def mask(payload, mask, acc) when is_integer(mask), do: mask(payload, <<mask::32>>, acc)
def mask(<<h::8, rest::binary>>, <<current::8, mask::24>>, acc) do
mask(rest, <<mask::24, current::8>>, acc <> <<Bitwise.bxor(h, current)::8>>)
end
def mask(<<>>, _mask, acc), do: acc
end
mask = 1234
Benchee.run(
%{
"old" => fn input -> Bench.orig(input, mask) end,
"new" => fn input -> Bench.mask(input, mask) end,
},
time: 10,
memory_time: 2,
inputs: %{
"small" => String.duplicate("a", 1_000),
"medium" => String.duplicate("a", 100_000),
"large" => String.duplicate("a", 10_000_000)
}
)
Operating System: Linux
CPU Information: AMD Ryzen 7 3700X 8-Core Processor
Number of Available Cores: 16
Available memory: 15.54 GB
Elixir 1.14.0
Erlang 25.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 2 s
reduction time: 0 ns
parallel: 1
inputs: large, medium, small
Estimated total run time: 1.40 min
Benchmarking new with input large ...
Benchmarking new with input medium ...
Benchmarking new with input small ...
Benchmarking old with input large ...
Benchmarking old with input medium ...
Benchmarking old with input small ...
##### With input large #####
Name ips average deviation median 99th %
new 1.19 0.84 s ±3.93% 0.85 s 0.90 s
old 0.110 9.07 s ±7.40% 9.07 s 9.55 s
Comparison:
new 1.19
old 0.110 - 10.79x slower +8.23 s
Memory usage statistics:
Name Memory usage
new 1.71 GB
old 5.88 GB - 3.43x memory usage +4.17 GB
**All measurements for memory usage were the same**
##### With input medium #####
Name ips average deviation median 99th %
new 133.36 7.50 ms ±5.13% 7.47 ms 8.62 ms
old 12.80 78.13 ms ±3.27% 78.46 ms 80.64 ms
Comparison:
new 133.36
old 12.80 - 10.42x slower +70.63 ms
Memory usage statistics:
Name Memory usage
new 17.55 MB
old 58.99 MB - 3.36x memory usage +41.44 MB
**All measurements for memory usage were the same**
##### With input small #####
Name ips average deviation median 99th %
new 6.51 K 153.69 μs ±54.83% 154.69 μs 348.61 μs
old 2.20 K 453.62 μs ±17.34% 450.90 μs 623.49 μs
Comparison:
new 6.51 K
old 2.20 K - 2.95x slower +299.93 μs
Memory usage statistics:
Name Memory usage
new 180.52 KB
old 616.16 KB - 3.41x memory usage +435.63 KB
**All measurements for memory usage were the same**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment