[Haskell-cafe] How to write fast for loops
jwlato at gmail.com
Mon Apr 28 00:34:09 UTC 2014
On Sun, Apr 27, 2014 at 4:49 PM, 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!
It can make a difference in that, with unboxed vectors, the compiler can
statically determine that it is able to use unboxed values, and therefore
is more likely to do so. Having finally broken down and run some tests, I
can report that on my system using V.enumFromTo with unboxed vectors
results in the same performance as the hand-written loop.
> > 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.
I believe that if you check this with ghc-7.6.3 and -O2, you will discover
that my analysis is correct :)
However, I like Conrad's suggestion, looks like an interesting library.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe