Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
enricosada opened this issue Apr 22, 2015 · 9 comments
Closed

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

enricosada opened this issue Apr 22, 2015 · 9 comments

Comments

@enricosada
Copy link
Contributor

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

@braden
Copy link
Contributor

braden commented Apr 22, 2015

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

@latkin
Copy link
Contributor

latkin commented Apr 22, 2015

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.

@enricosada
Copy link
Contributor Author

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

@latkin
Copy link
Contributor

latkin commented Apr 22, 2015

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

@mrange
Copy link
Contributor

mrange commented Apr 22, 2015

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

@mrange
Copy link
Contributor

mrange commented Apr 22, 2015

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.

@enricosada
Copy link
Contributor Author

@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 😄

@mrange
Copy link
Contributor

mrange commented Apr 22, 2015

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.

@enricosada
Copy link
Contributor Author

@mrange sent feedback! thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants