I'm trying to implement utf8 decoding in Ocaml as a learning project. To check the performance I'm benchmarking against the go standard library.
This is the go code:
package main
import (
"fmt"
"time"
"unicode/utf8"
)
func main() {
start := time.Now()
for i := 0; i < 1000000000; i++ {
utf8.ValidRune(23450)
}
elapsed := time.Since(start)
fmt.Println(elapsed)
}
When I run it, I get:
go build b.go
./b
344.979492ms
I decided to write an equivalent in ocaml:
let min = 0x0000
let max = 0x10FFFF
let surrogateMin = 0xD800
let surrogateMax = 0xDFFF
let validUchar c =
if (0 <= c && c < surrogateMin) then
true
else if (surrogateMax < c && c <= max) then
true
else
false
let time f x =
let t = Sys.time () in
let _ = f x in
let t2 = Sys.time () in
let diff = (t2 -. t) *. 1000. in
print_endline ((string_of_float diff) ^ "ms")
let test () =
for i = 0 to 1000000000 do
let _ = validUchar 23450 in
()
done
let () = time test ()
Output:
ocamlopt bMl.ml -o bMl
./bMl
2041.075ms
The ocaml equivalent basically copies the implementation of the go stdlib from https://golang.org/src/unicode/utf8/utf8.go#L517
Why is the ocaml code so much slower?