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










2















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









share|improve this question
























  • If you ask faster, what exactly prevents you from test and measure?

    – Hynek -Pichi- Vychodil
    Mar 7 at 7:57
















2















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









share|improve this question
























  • If you ask faster, what exactly prevents you from test and measure?

    – Hynek -Pichi- Vychodil
    Mar 7 at 7:57














2












2








2








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    3














    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.






    share|improve this answer



























      3














      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.






      share|improve this answer

























        3












        3








        3







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 9:52









        sbacarosbacaro

        313311




        313311





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived