[Haskell-cafe] How to write fast for loops

Niklas Hambüchen mail at nh2.me
Sun Apr 27 23:49:05 UTC 2014


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.


More information about the Haskell-Cafe mailing list