Created
September 28, 2015 21:56
-
-
Save timholy/fed671c2261288e3c786 to your computer and use it in GitHub Desktop.
Performance testing of ForwardDiff and the role of parentheses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ForwardDiff | |
Base.real(x::ForwardDiff.GradientNumber) = real(x.value) | |
SimpleRatio(i,j) = i/j | |
function mygetindex_slow(itp::Matrix, x_1::Number, x_2::Number) | |
begin | |
begin # line 34: | |
ix_1 = clamp(round(Int,real(x_1)),1,size(itp,1)) # line 35: | |
fx_1 = x_1 - ix_1 # line 36: | |
ix_1 += 0 # line 37: | |
ixp_1 = min(size(itp,1),ix_1 + 1) # line 38: | |
ixm_1 = max(1,ix_1 - 1) | |
end | |
begin # line 34: | |
ix_2 = clamp(round(Int,real(x_2)),1,size(itp,2)) # line 35: | |
fx_2 = x_2 - ix_2 # line 36: | |
ix_2 += 0 # line 37: | |
ixp_2 = min(size(itp,2),ix_2 + 1) # line 38: | |
ixm_2 = max(1,ix_2 - 1) | |
end | |
end # line 18: | |
begin | |
begin # line 46: | |
cm_1 = sqr(fx_1 - SimpleRatio(1,2)) / 2 # line 47: | |
c_1 = SimpleRatio(3,4) - sqr(fx_1) # line 48: | |
cp_1 = sqr(fx_1 + SimpleRatio(1,2)) / 2 | |
end | |
begin # line 46: | |
cm_2 = sqr(fx_2 - SimpleRatio(1,2)) / 2 # line 47: | |
c_2 = SimpleRatio(3,4) - sqr(fx_2) # line 48: | |
cp_2 = sqr(fx_2 + SimpleRatio(1,2)) / 2 | |
end | |
end # line 21: | |
begin | |
@inbounds begin | |
itp1 = (cm_2 * itp[ixm_1,ixm_2]) + (c_2 * itp[ixm_1,ix_2]) + (cp_2 * itp[ixm_1,ixp_2]) | |
itp2 = (cm_2 * itp[ix_1,ixm_2]) + (c_2 * itp[ix_1,ix_2]) + (cp_2 * itp[ix_1,ixp_2]) | |
itp3 = (cm_2 * itp[ixp_1,ixm_2]) + (c_2 * itp[ixp_1,ix_2]) + (cp_2 * itp[ixp_1,ixp_2]) | |
ret = (cm_1 * itp1) + (c_1 * itp2) + (cp_1 * itp3) | |
end | |
end # line 22: | |
ret | |
end | |
function mygetindex_fast(itp::Matrix, x_1::Number, x_2::Number) | |
begin | |
begin # line 34: | |
ix_1 = clamp(round(Int,real(x_1)),1,size(itp,1)) # line 35: | |
fx_1 = x_1 - ix_1 # line 36: | |
ix_1 += 0 # line 37: | |
ixp_1 = min(size(itp,1),ix_1 + 1) # line 38: | |
ixm_1 = max(1,ix_1 - 1) | |
end | |
begin # line 34: | |
ix_2 = clamp(round(Int,real(x_2)),1,size(itp,2)) # line 35: | |
fx_2 = x_2 - ix_2 # line 36: | |
ix_2 += 0 # line 37: | |
ixp_2 = min(size(itp,2),ix_2 + 1) # line 38: | |
ixm_2 = max(1,ix_2 - 1) | |
end | |
end # line 18: | |
begin | |
begin # line 46: | |
cm_1 = sqr(fx_1 - SimpleRatio(1,2)) / 2 # line 47: | |
c_1 = SimpleRatio(3,4) - sqr(fx_1) # line 48: | |
cp_1 = sqr(fx_1 + SimpleRatio(1,2)) / 2 | |
end | |
begin # line 46: | |
cm_2 = sqr(fx_2 - SimpleRatio(1,2)) / 2 # line 47: | |
c_2 = SimpleRatio(3,4) - sqr(fx_2) # line 48: | |
cp_2 = sqr(fx_2 + SimpleRatio(1,2)) / 2 | |
end | |
end # line 21: | |
begin | |
@inbounds begin | |
itp1 = ((cm_2 * itp[ixm_1,ixm_2]) + (c_2 * itp[ixm_1,ix_2])) + (cp_2 * itp[ixm_1,ixp_2]) | |
itp2 = ((cm_2 * itp[ix_1,ixm_2]) + (c_2 * itp[ix_1,ix_2])) + (cp_2 * itp[ix_1,ixp_2]) | |
itp3 = ((cm_2 * itp[ixp_1,ixm_2]) + (c_2 * itp[ixp_1,ix_2])) + (cp_2 * itp[ixp_1,ixp_2]) | |
ret = ((cm_1 * itp1) + (c_1 * itp2)) + (cp_1 * itp3) | |
end | |
end # line 22: | |
ret | |
end | |
sqr(x) = x*x | |
function sumA_slow(itp, dz, irng, jrng) | |
s = zero(eltype(dz)) | |
dx, dy = dz[1], dz[2] | |
for j in jrng | |
for i in irng | |
s += mygetindex_slow(itp, i+dx, j+dy) | |
end | |
end | |
s | |
end | |
function sumA_fast(itp, dz, irng, jrng) | |
s = zero(eltype(dz)) | |
dx, dy = dz[1], dz[2] | |
for j in jrng | |
for i in irng | |
s += mygetindex_fast(itp, i+dx, j+dy) | |
end | |
end | |
s | |
end | |
A = rand(3,3) | |
irng = linspace(1.5,2.4999,1000) | |
jrng = linspace(1.5,2.4999,1000) | |
println("Warmup @time") | |
@time 1 | |
g = ForwardDiff.gradient(dz->sumA_slow(A, dz, irng, jrng)) | |
dz = rand(2) | |
g(dz) | |
println("Without parentheses:") | |
@time g(dz) | |
g = ForwardDiff.gradient(dz->sumA_fast(A, dz, irng, jrng)) | |
dz = rand(2) | |
g(dz) | |
println("With parentheses:") | |
@time g(dz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment