OCaml faster than Rust
-
Rust is a new "systems programming language" that is claimed to be
"blazingly fast". We put it to the test, pitting it against the F#
programming languag...
4 years ago
Putting the fun in functional programming since 2005!
cosine(x) = 1 - x²/2! + x⁴/4! - x⁶/6! + ...
float
-specific version in F#:let cosine n x =
let rec loop i q t c =
if i=n then c else
loop (i + 1) (q + 10 + 8*i) (-t * x * x / float q) (c + t)
loop 0 2 1.0 0.0
cosine 1000000 0.1
inline
in order to remove the performance overhead of this parameterization:let inline cosine zero one ofInt ( ~-. ) ( +. ) ( *. ) ( /. ) n x =
let rec loop i q t c =
if i=n then c else
loop (i + 1) (q + 10 + 8*i) (-.t *. x *. x /. ofInt q) (c +. t)
loop 0 2 one zero
float
like this, which is just as fast as before:cosine 0.0 1.0 float ( ~- ) (+) (*) (/) 1000000 0.1
float
:cosine 0.0f 1.0f float32 ( ~- ) (+) (*) (/) 1000000 0.1f
cosine 0N 1N BigNum.FromInt (~-) (+) (*) (/) 10 (1N / 10N)
type Expr =
| Int of int
| Var of string
| Add of Expr * Expr
| Mul of Expr * Expr
| Pow of Expr * Expr
static member (~-) f = Mul(Int -1, f)
static member (+) (f, g) = Add(f, g)
static member (*) (f, g) = Mul(f, g)
static member (/) (f, g) = Mul(f, Pow(g, Int -1))
cosine (Int 0) (Int 1) Int (~-) (+) (*) (/) 3 (Var "x")
-x*x
out of loop
.