After seeing an interesting lecture by Phil Trelford
https://www.youtube.com/watch?v=hx2vOwbB-X0
I was intrigued about the possibility of accelerating my code by replacing lists with arrays and more generally, of using mutable variables. So I did a simple test:
let xs = [0.0..1000000.00]
let axs = List.toArray xs
let f x = sin (x * x)
List.map f xs // Real: 00:00:00.170, CPU: 00:00:00.187, GC gen0: 5, gen1: 3, gen2: 1
Array.map f axs // Real: 00:00:00.046, CPU: 00:00:00.046, GC gen0: 0, gen1: 0, gen2: 0
Mapping through the array was more than three times faster than mapping through the list. At this point I have not yet tested the speed difference when the function called is more computationally intensive. The difference could be due just to being faster to move through the items in an array, and could become insignificant when each iteration is computationally intensive.
Still, there must be cases in which using arrays or more generally mutable variables could make a significant difference.
Before changing my code to use arrays instead of lists I would like to have a clearer picture of the consequences when the code is parallelized.
In general, when can one use mutable variables without risking having problems with parallelized code? Is there a simple test that would allow me to ascertain the robustness of a function when called in parallel?