[Haskell-cafe] How to write fast for loops

Conrad Parker conrad at metadecks.org
Mon Apr 28 00:00:00 UTC 2014


just for fun, and seeing as your goal is to make something that works
elegeantly and efficiently with list syntax, how about making a new type
that's an instance of OverloadedLists but never actually allocates
anything, so you can basically desugar "X.forM_ [a..b]" to your loop


On 28 April 2014 09:49, Niklas Hambüchen <mail at nh2.me> wrote:

> On 28/04/14 00:22, John Lato wrote:
> > Are unboxed vectors faster?  My rule of thumb is to use them over
> > Data.Vector whenever possible.
> I haven't checked yet, but should it matter?
> Because my goal is that the vector never be created *at all*, and boxed
> or not shouldn't make a difference on that!
> > I would expect it's because you never force the argument.  With
> > `enumFromTo` the argument is forced because it needs to be checked for
> > termination, but `enumFromN` is probably building up a big chain of
> > thunks.  I guess for this case `enumFromN` has no benefit over
> > `enumFromTo` because the intention is to create a single loop instead of
> > actually allocating the vector, so the warning in the documentation
> > doesn't necessarily apply.
> Also haven't checked that yet, but I suspect that instead of something
> thunk-related, the thing plainly allocates the vector.
> Just to clarify: `V.enumFromTo` works much better than `V.enumFromN`
> because in contrast to the latter it doesn't actually try to create the
> fully sized vector.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140428/5fca8160/attachment.html>

More information about the Haskell-Cafe mailing list