Skip to content

Optimize for i in [0 .. 10] do something #377

Closed

Description

@enricosada

from Stackoverflow

from @tpetricek tests:

for i in [ 0 .. 10000000 ] do // 3194ms (yikes!)
  last <- i

for i in 0 .. 10000000 do     // 3ms
  last <- i

for i = 0 to 10000000 do      // 3ms
  last <- i

for i in seq { 0 .. 10000000 } do // 709ms (smaller yikes!)
  last <- i

I added this because there is a really good walkthrough by stackoverflow user fulesnabel

with his answer i think is can be a upforgrab performance tags

Activity

  1. braden commented on Apr 22, 2015

    @braden
    Contributor

    non-integer for loops, and loops with non-1 steps are also a problem for the same reason:
    for i in 0.0f .. 1000000.0f
    for i in 0 .. 2 .. intForLoopIterations

  2. latkin commented on Apr 22, 2015

    @latkin
    Contributor

    for i in [0 .. 10000000] do is really a black hole because (A) it allocates a full 10M-element list, then (B) walks it using IEnumerable. #219 addresses part (B) at least.

    I'm tempted to blame the user for part (A) - they asked for a list, so they got a list... That's why the for i in 1 .. 10000000 do syntax is there. Do developers not realize [a .. b] creates a list?

    @braden yes, there are a bunch of gaps there, which might represent relatively low-hanging fruit for anyone interested in codegen optimizations.

  3. enricosada commented on Apr 22, 2015

    @enricosada
    ContributorAuthor

    yes, a list is a list 😄 but is an easy target for an optimization.
    The real gold here is the walkthrough, is really good, can be a base guide for multiple similar optimizations.
    I cannot find the github account of user fulesnabel, a shame

  4. latkin commented on Apr 22, 2015

    @latkin
    Contributor

    Agreed, great walkthrough. User's profile mentions T4, I wonder if it is @mrange?

  5. mrange commented on Apr 22, 2015

    @mrange
    Contributor

    Yes it was me that wrote that SO post. I am actually working the SO post into something of a blog post explaining (hopefully) how to do a expression optimization. The blog post will contain functionality and IL generation tests as well. The plan is to end the blog post by posting it as a PR to F# (with the possibility it gets rejected which is also a good learning ;))

  6. mrange commented on Apr 22, 2015

    @mrange
    Contributor

    WRT to for i in 0.0f .. 1000000.0f, for i in 0 .. 2 .. intForLoopIterations when looking into this earlier these are more intrusive.

    IIRC these changes required changes the AST and pickle.fs which always scares me.

  7. enricosada commented on Apr 22, 2015

    @enricosada
    ContributorAuthor

    @mrange please do it
    the post will become a really good intro on FSharp compiler, i really liked the style
    I was going to ask you about a pr on SO , but thx latkin 😄

  8. mrange commented on Apr 22, 2015

    @mrange
    Contributor

    In case you are interested in giving feedback on the blog post please see: https://mrange.wordpress.com/?p=706&shareadraft=5536c7f96b8a5

    The SO post was skimped over how to do testing which I included in the blog. That of course made it bigger. @sergey_tihon has some feedback where he was asking for a better introduction so I tried to address that to.

  9. enricosada commented on Apr 23, 2015

    @enricosada
    ContributorAuthor

    @mrange sent feedback! thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions