Nope. I’ll try to pop a single item off the enumerable and if there is one returns false otherwise the enumerable is empty. It doesn’t enumerate everything.
Even if it’s constant time it’s still more work though:
map = for i <- 1..5000, into: %{}, do: {i, System.unique_integer()}
Benchee.run(%{
"map_size" => fn -> map_size(map) > 0 end,
"pattern" => fn -> map != %{} end
})
Operating System: Linux
CPU Information: AMD Ryzen 3 3200G with Radeon Vega Graphics
Number of Available Cores: 4
Available memory: 5.80 GB
Elixir 1.13.2
Erlang 24.1.7
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking map_size ...
Benchmarking pattern ...
Name ips average deviation median 99th %
pattern 1.45 M 0.69 μs ±5168.36% 0.47 μs 0.96 μs
map_size 0.93 M 1.07 μs ±2783.37% 0.77 μs 1.65 μs
Comparison:
pattern 1.45 M
map_size 0.93 M - 1.56x slower +0.38 μs






















