Skip to content

Instantly share code, notes, and snippets.

@include-yy
Last active November 20, 2022 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save include-yy/37fd8f206719202ab30fe8e77cff7df7 to your computer and use it in GitHub Desktop.
Save include-yy/37fd8f206719202ab30fe8e77cff7df7 to your computer and use it in GitHub Desktop.
test Rescript uncurry and array unsafe operation

对计算函数的反柯里化测试

  • c 后缀表示去柯里化
  • unsafe 后缀表示使用了无检查的数组操作
  • add 后缀表示直接进行加法,不使用加法函数
  • s 后缀表示带副作用操作
  • n 后缀表示创建新的结果向量,不使用默认提供的结果向量
let n = 10000000
let a = Js.Vector.make(100, 0.0)
let b = Js.Vector.make(100, 0.0)
let c = Js.Vector.make(100, 0.0)
let add = (x, y) => x +. y
let add_c = (. x, y) => x +. y
let test = (name, f) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(a, b))
}
Js.Console.timeEnd(name)
}
let test_f = (name, f, fa) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(a, b, fa))
}
Js.Console.timeEnd(name)
}
let test_c = (name, f) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(. a, b))
}
Js.Console.timeEnd(name)
}
let test_f_c = (name, f, fa) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(. a, b, fa))
}
Js.Console.timeEnd(name)
}
let f = (a, b, f) => a->Js.Array2.mapi((_, i) => f(a[i],b[i]))
let f_c = (. a, b, f) => a->Js.Array2.mapi((_, i) => f(. a[i],b[i]))
let f_unsafe_get = (a, b, f) => a->Js.Array2.mapi((_, i) => f(Js.Array2.unsafe_get(a,i), Js.Array2.unsafe_get(b, i)))
let f_unsafe_get_c = (. a, b, f) => a->Js.Array2.mapi((_, i) => f(. Js.Array2.unsafe_get(a,i), Js.Array2.unsafe_get(b, i)))
let f_add = (a, b) => a->Js.Array2.mapi((_, i) => a[i] +. b[i])
let f_add_c = (. a, b) => a->Js.Array2.mapi((_, i) => a[i] +. b[i])
let f_add_unsafe_get = (a, b) => a->Js.Array2.mapi((_, i) => Js.Array2.unsafe_get(a,i) +. Js.Array2.unsafe_get(b, i))
let f_add_unsafe_get_c = (. a, b) => a->Js.Array2.mapi((_, i) => Js.Array2.unsafe_get(a,i) +. Js.Array2.unsafe_get(b, i))
let fs = (a, b, f) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
c[i] = f(a[i], b[i])
}
c
}
let fsn = (a, b, f) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
c[i] = f(a[i], b[i])
}
c
}
let fs_unsafe = (a, b, f) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, f(Js.Array2.unsafe_get(a, i), Js.Array2.unsafe_get(b, i)))
}
c
}
let fsn_unsafe = (a, b, f) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, f(Js.Array2.unsafe_get(a, i), Js.Array2.unsafe_get(b, i)))
}
c
}
let fs_c = (. a, b, f) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
c[i] = f(. a[i], b[i])
}
c
}
let fsn_c = (. a, b, f) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
c[i] = f(. a[i], b[i])
}
c
}
let fs_unsafe_c = (. a, b, f) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, f(. Js.Array2.unsafe_get(a, i), Js.Array2.unsafe_get(b, i)))
}
c
}
let fsn_unsafe_c = (. a, b, f) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, f(. Js.Array2.unsafe_get(a, i), Js.Array2.unsafe_get(b, i)))
}
c
}
let fs_add = (a, b) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
c[i] = a[i] +. b[i]
}
c
}
let fsn_add = (a, b) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
c[i] = a[i] +. b[i]
}
c
}
let fs_add_unsafe = (a, b) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, Js.Array2.unsafe_get(a, i) +. Js.Array2.unsafe_get(b, i))
}
c
}
let fsn_add_unsafe = (a, b) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, Js.Array2.unsafe_get(a, i) +. Js.Array2.unsafe_get(b, i))
}
c
}
let fs_add_c = (. a, b) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
c[i] = a[i] +. b[i]
}
c
}
let fsn_add_c = (. a, b) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
c[i] = a[i] +. b[i]
}
c
}
let fs_add_unsafe_c = (. a, b) => {
let len = a->Js.Array2.length
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, Js.Array2.unsafe_get(a, i) +. Js.Array2.unsafe_get(b, i))
}
c
}
let fsn_add_unsafe_c = (. a, b) => {
let len = a->Js.Array2.length
let c = Js.Vector.make(len, 0.0)
for i in 0 to len - 1 {
Js.Array2.unsafe_set(c, i, Js.Array2.unsafe_get(a, i) +. Js.Array2.unsafe_get(b, i))
}
c
}
Js.Console.timeStart("test")
Js.log("######################")
Js.log("# no side effect now #")
Js.log("######################")
test_f("f", f, add)
test_f("f_unsafte_get", f_unsafe_get, add)
test("f_add", f_add)
test("f_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("f_c", f_c, add_c)
test_f_c("f_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("f_add_c", f_add_c)
test_c("f_add_unsafe_get_c", f_add_unsafe_get_c)
Js.log("######################")
Js.log("# side effect start! #")
Js.log("######################")
Js.log("|||no create new out vector|||")
test_f("fs", f, add)
test_f("fs_unsafte_get", f_unsafe_get, add)
test("fs_add", f_add)
test("fs_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("fs_c", f_c, add_c)
test_f_c("fs_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("fs_add_c", f_add_c)
test_c("fs_add_unsafe_get_c", f_add_unsafe_get_c)
Js.log("----------------------")
Js.log("|||create new out vector|||")
test_f("fsn", f, add)
test_f("fsn_unsafte_get", f_unsafe_get, add)
test("fsn_add", f_add)
test("fsn_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("fsn_c", f_c, add_c)
test_f_c("fsn_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("fsn_add_c", f_add_c)
test_c("fsn_add_unsafe_get_c", f_add_unsafe_get_c)
Js.Console.timeEnd("test")
######################
# no side effect now #
######################
f: 17.353s
f_unsafte_get: 17.517s
f_add: 3.269s
f_add_unsafe_get: 2.913s
CCCCCCCCCCCCCCCCCCCCCC
f_c: 3.343s
f_unsafe_get_c: 3.225s
f_add_c: 3.225s
f_add_unsafe_get_c: 2.746s
######################
# side effect start! #
######################
|||no create new out vector|||
fs: 17.243s
fs_unsafte_get: 17.375s
fs_add: 3.257s
fs_add_unsafe_get: 2.872s
CCCCCCCCCCCCCCCCCCCCCC
fs_c: 3.270s
fs_unsafe_get_c: 3.256s
fs_add_c: 3.139s
fs_add_unsafe_get_c: 2.733s
----------------------
|||create new out vector|||
fsn: 17.255s
fsn_unsafte_get: 17.362s
fsn_add: 3.251s
fsn_add_unsafe_get: 2.859s
CCCCCCCCCCCCCCCCCCCCCC
fsn_c: 3.274s
fsn_unsafe_get_c: 3.229s
fsn_add_c: 3.156s
fsn_add_unsafe_get_c: 2.755s
test: 2:39.888 (m:ss.mmm)
open Js.TypedArray2
let n = 10000000
let a = Js.Vector.make(100, 0.0)
let b = Js.Vector.make(100, 0.0)
let c = Js.Vector.make(100, 0.0)
let a = Float64Array.make(a)
let b = Float64Array.make(b)
let c = Float64Array.make(c)
let t = Js.Vector.make(100, 0.0)
let add = (x, y) => x +. y
let add_c = (. x, y) => x +. y
let test = (name, f) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(a, b))
}
Js.Console.timeEnd(name)
}
let test_f = (name, f, fa) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(a, b, fa))
}
Js.Console.timeEnd(name)
}
let test_c = (name, f) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(. a, b))
}
Js.Console.timeEnd(name)
}
let test_f_c = (name, f, fa) => {
Js.Console.timeStart(name)
for _ in 0 to n - 1 {
ignore(f(. a, b, fa))
}
Js.Console.timeEnd(name)
}
let f_unsafe_get = (a, b, f) => a->Float64Array.mapi((. _, i) => f(Float64Array.unsafe_get(a,i), Float64Array.unsafe_get(b, i)))
let f_unsafe_get_c = (. a, b, f) => a->Float64Array.mapi((. _, i) => f(. Float64Array.unsafe_get(a,i), Float64Array.unsafe_get(b, i)))
let f_add_unsafe_get = (a, b) => a->Float64Array.mapi((. _, i) => Float64Array.unsafe_get(a,i) +. Float64Array.unsafe_get(b, i))
let f_add_unsafe_get_c = (. a, b) => a->Float64Array.mapi((. _, i) => Float64Array.unsafe_get(a,i) +. Float64Array.unsafe_get(b, i))
let fs_unsafe = (a, b, f) => {
let len = a->Float64Array.length
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, f(Float64Array.unsafe_get(a, i), Float64Array.unsafe_get(b, i)))
}
c
}
let fsn_unsafe = (a, b, f) => {
let len = a->Float64Array.length
let c = Float64Array.make(t)
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, f(Float64Array.unsafe_get(a, i), Float64Array.unsafe_get(b, i)))
}
c
}
let fs_unsafe_c = (. a, b, f) => {
let len = a->Float64Array.length
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, f(. Float64Array.unsafe_get(a, i), Float64Array.unsafe_get(b, i)))
}
c
}
let fsn_unsafe_c = (. a, b, f) => {
let len = a->Float64Array.length
let c = Float64Array.make(t)
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, f(. Float64Array.unsafe_get(a, i), Float64Array.unsafe_get(b, i)))
}
c
}
let fs_add_unsafe = (a, b) => {
let len = a->Float64Array.length
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, Float64Array.unsafe_get(a, i) +. Float64Array.unsafe_get(b, i))
}
c
}
let fsn_add_unsafe = (a, b) => {
let len = a->Float64Array.length
let c = Float64Array.make(t)
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, Float64Array.unsafe_get(a, i) +. Float64Array.unsafe_get(b, i))
}
c
}
let fs_add_unsafe_c = (. a, b) => {
let len = a->Float64Array.length
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, Float64Array.unsafe_get(a, i) +. Float64Array.unsafe_get(b, i))
}
c
}
let fsn_add_unsafe_c = (. a, b) => {
let len = a->Float64Array.length
let c = Float64Array.make(t)
for i in 0 to len - 1 {
Float64Array.unsafe_set(c, i, Float64Array.unsafe_get(a, i) +. Float64Array.unsafe_get(b, i))
}
c
}
Js.Console.timeStart("test")
Js.log("######################")
Js.log("# no side effect now #")
Js.log("######################")
test_f("f_unsafte_get", f_unsafe_get, add)
test("f_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("f_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("f_add_unsafe_get_c", f_add_unsafe_get_c)
Js.log("######################")
Js.log("# side effect start! #")
Js.log("######################")
Js.log("|||no create new out vector|||")
test_f("fs_unsafte_get", f_unsafe_get, add)
test("fs_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("fs_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("fs_add_unsafe_get_c", f_add_unsafe_get_c)
Js.log("----------------------")
Js.log("|||create new out vector|||")
test_f("fsn_unsafte_get", f_unsafe_get, add)
test("fsn_add_unsafe_get", f_add_unsafe_get)
Js.log("CCCCCCCCCCCCCCCCCCCCCC")
test_f_c("fsn_unsafe_get_c", f_unsafe_get_c, add_c)
test_c("fsn_add_unsafe_get_c", f_add_unsafe_get_c)
Js.Console.timeEnd("test")
}
######################
# no side effect now #
######################
f_unsafte_get: 32.029s
f_add_unsafe_get: 13.576s
CCCCCCCCCCCCCCCCCCCCCC
f_unsafe_get_c: 13.245s
f_add_unsafe_get_c: 12.952s
######################
# side effect start! #
######################
|||no create new out vector|||
fs_unsafte_get: 30.436s
fs_add_unsafe_get: 13.433s
CCCCCCCCCCCCCCCCCCCCCC
fs_unsafe_get_c: 13.176s
fs_add_unsafe_get_c: 13.251s
----------------------
|||create new out vector|||
fsn_unsafte_get: 31.802s
fsn_add_unsafe_get: 15.036s
CCCCCCCCCCCCCCCCCCCCCC
fsn_unsafe_get_c: 14.586s
fsn_add_unsafe_get_c: 13.151s
test: 3:36.685 (m:ss.mmm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment