Elixir Enum.map vs For comprehension2019 Community Moderator ElectionDoes functional programming replace GoF design patterns?list comprehension vs. lambda + filterLarge-scale design in Haskell?Why are there two kinds of functions in Elixir?return first Key, Value from Map where Predicate is trueElixir check if map contains a list of keysReturn Map or Convert to Map? word-count elixirScoping problems with comprehensionsGetting Bad function Error in ElixirElixir nested lists with heterogeneous data being coerced randomly to strings when using Enum.map
The bar has been raised
In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?
A three room house but a three headED dog
They call me Inspector Morse
Why does the negative sign arise in this thermodynamic relation?
Good allowance savings plan?
Who deserves to be first and second author? PhD student who collected data, research associate who wrote the paper or supervisor?
Placing subfig vertically
Solving "Resistance between two nodes on a grid" problem in Mathematica
How strictly should I take "Candidates must be local"?
How much stiffer are 23c tires over 28c?
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Rejected in 4th interview round citing insufficient years of experience
How could our ancestors have domesticated a solitary predator?
How to pass a string to a command that expects a file?
String reversal in Python
Am I not good enough for you?
Is it true that real estate prices mainly go up?
Make a transparent 448*448 image
Upside Down Word Puzzle
MTG: Can I kill an opponent in response to lethal activated abilities, and not take the damage?
Are the terms "stab" and "staccato" synonyms?
Aliens englobed the Solar System: will we notice?
Built-In Shelves/Bookcases - IKEA vs Built
Elixir Enum.map vs For comprehension
2019 Community Moderator ElectionDoes functional programming replace GoF design patterns?list comprehension vs. lambda + filterLarge-scale design in Haskell?Why are there two kinds of functions in Elixir?return first Key, Value from Map where Predicate is trueElixir check if map contains a list of keysReturn Map or Convert to Map? word-count elixirScoping problems with comprehensionsGetting Bad function Error in ElixirElixir nested lists with heterogeneous data being coerced randomly to strings when using Enum.map
I have a map and I am modifying each element on it, I am confused which approach is better(faster) to do it with Enum.map
and then Enum.into(%)
or to use for comprehension like
for key, value <- my_map, into: % do
key, new_value
end
functional-programming erlang elixir
add a comment |
I have a map and I am modifying each element on it, I am confused which approach is better(faster) to do it with Enum.map
and then Enum.into(%)
or to use for comprehension like
for key, value <- my_map, into: % do
key, new_value
end
functional-programming erlang elixir
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57
add a comment |
I have a map and I am modifying each element on it, I am confused which approach is better(faster) to do it with Enum.map
and then Enum.into(%)
or to use for comprehension like
for key, value <- my_map, into: % do
key, new_value
end
functional-programming erlang elixir
I have a map and I am modifying each element on it, I am confused which approach is better(faster) to do it with Enum.map
and then Enum.into(%)
or to use for comprehension like
for key, value <- my_map, into: % do
key, new_value
end
functional-programming erlang elixir
functional-programming erlang elixir
edited Mar 7 at 8:30
fhdhsni
646615
646615
asked Mar 7 at 7:56
fadedfaded
5971724
5971724
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57
add a comment |
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57
add a comment |
1 Answer
1
active
oldest
votes
You can use Benchee to run this kind of comparisons.
A simple Benchee test will show that Enum
is faster for cases like this one.
iex(1)> m = %a: 1, b: 2, c: 3, d: 4
%a: 1, b: 2, c: 3, d: 4
iex(2)> with_enum = fn -> Enum.map(m, fn k, v -> k, v * v end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(3)> with_for = fn -> for k, v <- m, into: %, do: k, v * v end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(4)> Benchee.run(%
...(4)> "with_enum" => fn -> with_enum.() end,
...(4)> "with_for" => fn -> with_for.() end
...(4)> )
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.71 GB
Elixir 1.7.4
Erlang 21.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking with_enum...
Benchmarking with_for...
Name ips average deviation median 99th %
with_enum 28.27 K 35.37 μs ±16.16% 34.37 μs 55.21 μs
with_for 19.55 K 51.14 μs ±9.16% 50.08 μs 59.94 μs
Comparison:
with_enum 28.27 K
with_for 19.55 K - 1.45x slower
In general, for
isn't the best option for these cases in Elixir, it's best suited for list comprehensions, which it can do pretty fast and with an easy to read syntax.
Enum
's functions are optimized to handle these scenarios which are more iterative, like what you would do with a for
construct in other programming languages.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038704%2felixir-enum-map-vs-for-comprehension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Benchee to run this kind of comparisons.
A simple Benchee test will show that Enum
is faster for cases like this one.
iex(1)> m = %a: 1, b: 2, c: 3, d: 4
%a: 1, b: 2, c: 3, d: 4
iex(2)> with_enum = fn -> Enum.map(m, fn k, v -> k, v * v end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(3)> with_for = fn -> for k, v <- m, into: %, do: k, v * v end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(4)> Benchee.run(%
...(4)> "with_enum" => fn -> with_enum.() end,
...(4)> "with_for" => fn -> with_for.() end
...(4)> )
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.71 GB
Elixir 1.7.4
Erlang 21.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking with_enum...
Benchmarking with_for...
Name ips average deviation median 99th %
with_enum 28.27 K 35.37 μs ±16.16% 34.37 μs 55.21 μs
with_for 19.55 K 51.14 μs ±9.16% 50.08 μs 59.94 μs
Comparison:
with_enum 28.27 K
with_for 19.55 K - 1.45x slower
In general, for
isn't the best option for these cases in Elixir, it's best suited for list comprehensions, which it can do pretty fast and with an easy to read syntax.
Enum
's functions are optimized to handle these scenarios which are more iterative, like what you would do with a for
construct in other programming languages.
add a comment |
You can use Benchee to run this kind of comparisons.
A simple Benchee test will show that Enum
is faster for cases like this one.
iex(1)> m = %a: 1, b: 2, c: 3, d: 4
%a: 1, b: 2, c: 3, d: 4
iex(2)> with_enum = fn -> Enum.map(m, fn k, v -> k, v * v end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(3)> with_for = fn -> for k, v <- m, into: %, do: k, v * v end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(4)> Benchee.run(%
...(4)> "with_enum" => fn -> with_enum.() end,
...(4)> "with_for" => fn -> with_for.() end
...(4)> )
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.71 GB
Elixir 1.7.4
Erlang 21.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking with_enum...
Benchmarking with_for...
Name ips average deviation median 99th %
with_enum 28.27 K 35.37 μs ±16.16% 34.37 μs 55.21 μs
with_for 19.55 K 51.14 μs ±9.16% 50.08 μs 59.94 μs
Comparison:
with_enum 28.27 K
with_for 19.55 K - 1.45x slower
In general, for
isn't the best option for these cases in Elixir, it's best suited for list comprehensions, which it can do pretty fast and with an easy to read syntax.
Enum
's functions are optimized to handle these scenarios which are more iterative, like what you would do with a for
construct in other programming languages.
add a comment |
You can use Benchee to run this kind of comparisons.
A simple Benchee test will show that Enum
is faster for cases like this one.
iex(1)> m = %a: 1, b: 2, c: 3, d: 4
%a: 1, b: 2, c: 3, d: 4
iex(2)> with_enum = fn -> Enum.map(m, fn k, v -> k, v * v end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(3)> with_for = fn -> for k, v <- m, into: %, do: k, v * v end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(4)> Benchee.run(%
...(4)> "with_enum" => fn -> with_enum.() end,
...(4)> "with_for" => fn -> with_for.() end
...(4)> )
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.71 GB
Elixir 1.7.4
Erlang 21.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking with_enum...
Benchmarking with_for...
Name ips average deviation median 99th %
with_enum 28.27 K 35.37 μs ±16.16% 34.37 μs 55.21 μs
with_for 19.55 K 51.14 μs ±9.16% 50.08 μs 59.94 μs
Comparison:
with_enum 28.27 K
with_for 19.55 K - 1.45x slower
In general, for
isn't the best option for these cases in Elixir, it's best suited for list comprehensions, which it can do pretty fast and with an easy to read syntax.
Enum
's functions are optimized to handle these scenarios which are more iterative, like what you would do with a for
construct in other programming languages.
You can use Benchee to run this kind of comparisons.
A simple Benchee test will show that Enum
is faster for cases like this one.
iex(1)> m = %a: 1, b: 2, c: 3, d: 4
%a: 1, b: 2, c: 3, d: 4
iex(2)> with_enum = fn -> Enum.map(m, fn k, v -> k, v * v end) end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(3)> with_for = fn -> for k, v <- m, into: %, do: k, v * v end
#Function<20.127694169/0 in :erl_eval.expr/5>
iex(4)> Benchee.run(%
...(4)> "with_enum" => fn -> with_enum.() end,
...(4)> "with_for" => fn -> with_for.() end
...(4)> )
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
Number of Available Cores: 4
Available memory: 7.71 GB
Elixir 1.7.4
Erlang 21.0
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s
Benchmarking with_enum...
Benchmarking with_for...
Name ips average deviation median 99th %
with_enum 28.27 K 35.37 μs ±16.16% 34.37 μs 55.21 μs
with_for 19.55 K 51.14 μs ±9.16% 50.08 μs 59.94 μs
Comparison:
with_enum 28.27 K
with_for 19.55 K - 1.45x slower
In general, for
isn't the best option for these cases in Elixir, it's best suited for list comprehensions, which it can do pretty fast and with an easy to read syntax.
Enum
's functions are optimized to handle these scenarios which are more iterative, like what you would do with a for
construct in other programming languages.
answered Mar 7 at 9:52
sbacarosbacaro
313311
313311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038704%2felixir-enum-map-vs-for-comprehension%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you ask faster, what exactly prevents you from test and measure?
– Hynek -Pichi- Vychodil
Mar 7 at 7:57