Para hacer más corta las funciones anónimas se puede hacer así
Enum.map([1,2,3,4,5],&(&1*2))# [2,4,6,8,10]
Donde &(...) es la función y &1 es el primer parámetro de la función
La función de parada en una sola linea:
def print_multiple_times(_msg,0),do::ok
en tiempo de ejecución, cuál seria más rápida?
print_multiple_time(_msg, n) when n == 0, do: :ok
o
print_multiple_time(_msg, 0), do: :ok
Ambos modos son bastantes rápidos (en el orden de los nano segundos en una prueba rápida que hice). Si bien te diría que es muy temprano para preocuparte por el performance, te explico como puedes obtener algunos datos que podrían ayudarte a tomar una decisión en caso de ser necesario en el futuro.
Usando el paquete benchee y teniendo lo siguiente:
# lib/demo.exdefmoduleDemododeffirst(_msg, n)when n ==0,do::okdefsecond(_msg,0),do::okdefthird(_msg, n)when n ===0,do::okend
Nota que introduje un tercera función llamada third, en este caso en la guarda uso el operador de comparación estricto de Elixir, lo cual lo haría equivalente a la función second.
Puedes ver que tanto second y third son equivalentes en el número de millones de operaciones que procesan.
mix run bench.exs
Operating System: macOS
CPU Information: Apple M1 Max
Number of Available Cores: 10
Available memory: 64 GB
Elixir 1.13.4
Erlang 25.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: 42 s
Benchmarking first ...
Warning: The function you are trying to benchmark is super fast, making measurements more unreliable!
This holds especially true for memory measurements or when running with hooks.
See: https://github.com/bencheeorg/benchee/wiki/Benchee-Warnings#fast-execution-warning
You may disable this warning by passing print: [fast_warning: false] as configuration options.
Benchmarking second ...
Warning: The function you are trying to benchmark is super fast, making measurements more unreliable!
This holds especially true for memory measurements or when running with hooks.
See: https://github.com/bencheeorg/benchee/wiki/Benchee-Warnings#fast-execution-warning
You may disable this warning by passing print: [fast_warning: false] as configuration options.
Benchmarking third ...
Warning: The function you are trying to benchmark is super fast, making measurements more unreliable!
This holds especially true for memory measurements or when running with hooks.
See: https://github.com/bencheeorg/benchee/wiki/Benchee-Warnings#fast-execution-warning
You may disable this warning by passing print: [fast_warning: false] as configuration options.
Name ips average deviation median 99th %
second 278.89 M 3.59 ns ±237.57% 3.58 ns 3.71 ns
third 278.84 M 3.59 ns ±206.41% 3.58 ns 3.71 ns
first 159.99 M 6.25 ns ±20028.18% 4.20 ns 8.40 ns
Comparison:
second 278.89 M
third 278.84 M - 1.00x slower +0.00066 ns
first 159.99 M - 1.74x slower +2.66 ns
Memory usage statistics:
Name Memory usage
second 0 B
third 0 B - 1.00x memory usage +0 B
first 0 B - 1.00x memory usage +0 B
**All measurements for memory usage were the same**