If you're worried about performance, one of the important things to keep in mind is that F # doesnβt change anything by default. This requires copying in many naive implementations of algorithms, such as those described by you.
EDIT: I have no idea why, but simple tests of the following code give lower Array.map
results. Be sure to analyze any algorithm that you are trying to execute with such optimizations. However, I have very similar results between for
and map
.
Array.map
creates a new array for the result of the operation, instead you want Array.iteri
.
rgbPixels |> Array.iteri (fun ix -> rgbPixels.[i] <- x + 10uy)
Please note that this can be completed in your own module as shown below.
module ArrayM = let map fa = a |> Array.iteri (fun ix -> a.[i] <- fx)
Unfortunately, this is a necessary evil, since one of the main tenants of functional programming is to stick to immutable objects in the same way as your algorithm allows, and then somehow finish, change the mutation, where critical performance is. If you know that your performance is critical to you, you will need to start with such assistants.
Also note that there is probably a library that provides this functionality, I just don't know about it.
source share