I do not understand why F# so slow
let inline swap<'a> (a: byref<'a>, b: byref<'a>) =
let t = a
a <- b
b <- t
let sort arr =
let ln = Array.length arr
for i = 0 to ln - 1 do
for j = 0 to ln - 2 - i do
if arr.[j + 1] < arr.[j] then
swap (&arr.[j + 1], &arr.[j])
let doTest () =
let max = 100_000
let value = float (max)
let arr =
Array.init max (fun i -> value - float (i))
sort arr
printfn "%A" (arr.[0..5])
#time
doTest ()
It is very slow (few minutes).
Analog on C# take above few seconds. Why?
Original cod here vostok/example/BubbleSort.mod at master · Vostok-space/vostok (github.com)
(Interesting post about result for it on Oberon(simple language from Nicolaus Wirth) Восток: Скорость пузырьковой сортировки на Oberon, C++, Go, Rust (vostok-space.blogspot.com))
using System.Runtime.CompilerServices;
var m = 100_000;
var a = new double[m];
var s = (double)m;
for (var i = 0; i < m; i++)
{
a[i] = s;
s = s - 1;
}
Sort(a);
Console.WriteLine(a[0]);
static void Sort(double[] a)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Swap(ref double x, ref double y)
{
var t = x;
x = y;
y = t;
};
for (var i = 0; i < a.Length; i++)
for (var j = 0; j < (a.Length - 1 - i); j++)
if (a[j + 1] < a[j])
Swap(ref a[j + 1], ref a[j]);
}