Test your knowledge of Haskell's array primitives

Consider the following two implementations of a 'map' function for arrays. Can you guess which one of them is the fastest without running them? (Please ignore the uses of `unsafeCoerce` :P)... #haskell

https://kbin.social/m/haskell/t/352117

Test your knowledge of Haskell's array primitives - Haskell - kbin.social

Consider the following two implementations of a 'map' function for arrays. Can you guess which one of them is the fastest without running them? (Please ignore the uses of `unsafeCoerce` :P)...

You may want to use the Hackage page of the Data.Primitive.Array module: https://hackage.haskell.org/package/primitive-0.8.0.0/docs/Data-Primitive-Array.html

Some relevant functions:

createArray :: Int -> a -> (forall s. MutableArray s a -> ST s ()) -> Array a

Create an array of the given size with a default value, apply the monadic function and freeze the result. If the size is 0, return emptyArray (rather than a new copy thereof).

createArray 0 _ _ = emptyArray createArray n x f = runArray $ do mary <- newArray n x f mary pure mary thawArray :: PrimMonad m => Array a -- ^ source -> Int -- ^ offset -> Int -- ^ length -> m (MutableArray (PrimState m) a)

Create a mutable array from a slice of an immutable array.

This operation makes a copy of the specified slice, so it is safe to use the immutable array afterward.

Note: The provided array should contain the full subrange specified by the two Ints, but this is not checked.

Data.Primitive.Array

I've spent this evening trying to get conclusive benchmark results, but I have not succeeded with that yet. It seems like this is pretty difficult to benchmark. So I'd like to hear it if someone else is able to get good benchmark results.
I'm getting pretty consistent results like this now.