Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AS all of the conversion modules implement `convert/1`, it is unnecessary to perform a function capture. Executing a captured function, even a static capture is slower. I ran three slightly different tests with benchee. In all cases, there was a module that looked like this: ```elixir defmodule Mod do def target, do: nil def target(input), do: input def run_capture, do: exec_capture(&__MODULE__.target/0) def run_capture(input), do: exec_capture(input, &__MODULE__.target/0) def run_module, do: exec_module(__MODULE__) def run_module(input), do: exec_module(input, __MODULE__) end ``` 1. A list of 10,000 numbers: ```elixir list = Enum.to_list(1..10_0000) Benchee.run( %{ "cpature" => fn -> Mod.run_capture(list) end, "module" => fn -> Mod.run_module(list) end }, time: 10, memory_time: 2 ) ``` ```console $ elixir capturevsmodule.exs … Name ips average deviation median 99th % module 30.20 M 33.11 ns ±138.67% 42 ns 42 ns capture 25.07 M 39.88 ns ±61177.08% 42 ns 42 ns Comparison: module 30.20 M capture 25.07 M - 1.20x slower +6.77 ns … ``` 2. A short word: ```elixir word = "helloWorld" Benchee.run( %{ "cpature" => fn -> Mod.run_capture(word) end, "module" => fn -> Mod.run_module(word) end }, time: 10, memory_time: 2 ) ``` ```console $ elixir capturevsmodule.exs … Name ips average deviation median 99th % module 30.97 M 32.29 ns ±147.88% 42 ns 42 ns capture 28.35 M 35.27 ns ±34598.68% 42 ns 42 ns Comparison: module 30.97 M capture 28.35 M - 1.09x slower +2.98 ns … ``` 3: No input (just dispatch overhead): ```elixir Benchee.run( %{ "cpature" => fn -> Mod.run_capture() end, "module" => fn -> Mod.run_module() end }, time: 10, memory_time: 2 ) ``` ```console $ elixir capturevsmodule.exs … Name ips average deviation median 99th % module 30.13 M 33.19 ns ±136.81% 42 ns 42 ns capture 27.46 M 36.42 ns ±37965.35% 42 ns 42 ns Comparison: module 30.13 M capture 27.46 M - 1.10x slower +3.23 ns … ``` This will matter much more on `convert_nested` calls than on `convert_plain` calls, but either way, I see a 9–20% boost as an absolute win.
- Loading branch information