Skip to content

Instantly share code, notes, and snippets.

@cortner
Last active December 13, 2016 19:23
Show Gist options
  • Save cortner/9e2e52297141a3f6f3fdc8ac8e81fc41 to your computer and use it in GitHub Desktop.
Save cortner/9e2e52297141a3f6f3fdc8ac8e81fc41 to your computer and use it in GitHub Desktop.
minitest of FunctionWrappers.jl
#
# Test performance of FunctionWrappers.jl
#
f1(r) = exp(3.0*r)
f2(r) = cos(7*r)^2
# N evaluates of sin, cos; hard-coded
function test1(N)
r = 0.234; s = 0.0
for n = 1:N
s += f1(r) + f2(r)
end
return s
end
# pass sin, cos as tuple ff = (sin, cos)
function test2(N, ff)
r = 0.234; s = 0.0
for n = 1:N, f in ff
s += f(r)
end
return s
end
# return type asserted
function test3(N, ff)
r = 0.234; s = 0.0
for n = 1:N, f in ff
s += f(r)::Float64
end
return s
end
tup = (f1, f2)
test1(10)
test2(10, tup)
println("Test1: hard-coded functions")
@time test1(1_000_000)
@time test1(1_000_000)
@time test1(1_000_000)
println("Test2: pass functions in a tuple")
@time test2(1_000_000, tup)
@time test2(1_000_000, tup)
@time test2(1_000_000, tup)
println("Test3: types are asserted")
@time test3(1_000_000, tup)
@time test3(1_000_000, tup)
@time test3(1_000_000, tup)
import FunctionWrappers
import FunctionWrappers: FunctionWrapper
typealias F64fun FunctionWrapper{Float64,Tuple{Float64}}
wrapped = (F64fun(f1), F64fun(f2))
test2(10, wrapped)
println("Test 4: pass functions as tuple of FunctionWrappers")
@time test2(1_000_000, wrapped)
@time test2(1_000_000, wrapped)
@time test2(1_000_000, wrapped)
# OUTPUT
# Test1: hard-coded functions
# 0.023688 seconds (131 allocations: 7.734 KB)
# 0.024503 seconds (5 allocations: 176 bytes)
# 0.025517 seconds (5 allocations: 176 bytes)
# Test2: pass functions in a tuple
# 0.318166 seconds (6.00 M allocations: 91.553 MB, 52.69% gc time)
# 0.149604 seconds (6.00 M allocations: 91.553 MB, 2.44% gc time)
# 0.138901 seconds (6.00 M allocations: 91.553 MB, 3.08% gc time)
# Test3: types are asserted
# 0.118090 seconds (4.00 M allocations: 61.139 MB, 3.34% gc time)
# 0.107509 seconds (4.00 M allocations: 61.035 MB, 2.61% gc time)
# 0.108348 seconds (4.00 M allocations: 61.035 MB, 2.52% gc time)
# Test 4: pass functions as tuple of FunctionWrappers
# 0.034954 seconds (5 allocations: 176 bytes)
# 0.037139 seconds (5 allocations: 176 bytes)
# 0.035320 seconds (5 allocations: 176 bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment