On Wednesday, July 31, 2019 at 12:36:53 PM UTC-6, José Valim wrote:
>
> I thought about said approach but returning anonymous functions are not
> very common in Elixir (probably due to the lack of currying) and also less
> efficient. So I would prefer Function.identity/1.
>
Just for reference, one of my benchmarks:
```
Name ips average deviation median
99th %
Direct-Apply 23.02 M 0.0434 μs ±7.10% 0.0420 μs
0.0540 μs
Direct 22.77 M 0.0439 μs ±8.35% 0.0420 μs
0.0630 μs
Anon-Module 15.32 M 0.0653 μs ±105.61% 0.0600 μs
0.100 μs
Anon-Closure 15.21 M 0.0658 μs ±183.59% 0.0600 μs
0.100 μs
Anon-Empty 14.02 M 0.0713 μs ±7.08% 0.0690 μs
0.0920 μs
Indirect 12.49 M 0.0801 μs ±8.96% 0.0760 μs
0.110 μs
Indirect-Apply 12.49 M 0.0801 μs ±7.03% 0.0760 μs
0.100 μs
Comparison:
Direct-Apply 23.02 M
Direct 22.77 M - 1.01x slower
Anon-Module 15.32 M - 1.50x slower
Anon-Closure 15.21 M - 1.51x slower
Anon-Empty 14.02 M - 1.64x slower
Indirect 12.49 M - 1.84x slower
Indirect-Apply 12.49 M - 1.84x slower
Memory usage statistics:
Name Memory usage
Direct-Apply 280 B
Direct 280 B - 1.00x memory usage
Anon-Module 280 B - 1.00x memory usage
Anon-Closure 280 B - 1.00x memory usage
Anon-Empty 280 B - 1.00x memory usage
Indirect 280 B - 1.00x memory usage
Indirect-Apply 280 B - 1.00x memory usage
```
Direct's are direct calls, like `SomeModule.somefunc()`, Indirects are
`module_in_variable.somefunc()` (and apply is the `apply/3` function),
anon-module is of the `&SomeModule.somefunc/0` form, anon-closure is
capturing local non-static state and returning that, and anon-empty is
just `fn() -> nil end`. No clue why anon-empty is the slowest but that's
very consistent on these runs no matter how many times I run the benchmark
on multiple computers.
So module calls are the fastest by an almost imperceptible amount. Then
again you can just return a module call from the function itself... ^.^
And I think it's not the currying in Elixir is why, I actually do return
quite a few anonymous functions in a lot of my code, it's easy enough, it's
just that it's not the 'style' of many elixir'ists, many of them don't come
from functional languages so they don't think in a functional programming
style. Functional programming is not just focusing on the functions, it is
the transformation of the functions as well, which isn't really done in
'most' of the elixir ecosystem.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/bd74e77f-6a4a-4aef-9164-628a32c27e4d%40googlegroups.com.