Last active
April 26, 2020 05:25
-
-
Save JeffreySarnoff/3ab123ef9a2f309472dbd152b2f2b266 to your computer and use it in GitHub Desktop.
ascertain the performance of `fn(xs::T1)` relative to `fn(xs::T2)`
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
# enable benchmarking, force paramter presets | |
try | |
using BenchmarkTools | |
BTools=BenchmarkTools.DEFAULT_PARAMETERS; | |
BTools.overhead = BenchmarkTools.estimate_overhead(); | |
BTools.evals=1; BTools.samples = 7000; | |
BTools.time_tolerance = 1e-8; | |
catch | |
error("BenchmarkTools.jl not found") | |
end | |
# introduce `@refd` benchmarking assist (`@refd @btime ..`) | |
try | |
import MacroTools: postwalk | |
using MacroTools: MacroTools, prewalk, postwalk, @capture | |
walk(x, inner, outer) = outer(x) | |
walk(x::Expr, inner, outer) = outer(Expr(x.head, map(inner, x.args)...)) | |
MacroTools.postwalk(f, x) = walk(x, x -> postwalk(f, x), f) | |
function _refd(expr::Expr) | |
if expr.head == :$ | |
:($(Expr(:$, :(Ref($(expr.args...)))))[]) | |
else | |
expr | |
end | |
end | |
catch | |
postwalk(a, b) = missing | |
error("MacroTools.jl not found") | |
end | |
_refd(x) = x | |
macro refd(expr) | |
out = postwalk(_refd, expr) |> esc | |
end | |
#= | |
`relspeed` determines the datatype-relative performance of a function | |
- (speedof fn(data::TypeA)) / (speedof fn(data::TypeB)) | |
- TypeB(TypeA(data)) == TypeB(data) | |
- TypeA(TypeB(data)) == TypeA(data) | |
- (speedof fn(xdata::TypeA, ydata::T)) / (speedof fn(xdata::TypeB, ydata::T)) where T | |
- TypeB(TypeA(data)) == TypeB(data) | |
- TypeA(TypeB(data)) == TypeA(data) | |
- (speedof fn(xdata::TypeA, ydata::TypeB)) / (speedof fn(xdata::TypeC, ydata::TypeD)) where .. | |
- TypeA(TypeC(xdata)) == TypeA(xdata) && TypeC(TypeA(xdata)) == TypeC(xdata) | |
- TypeB(TypeD(ydata)) == TypeB(ydata) && TypeD(TypeB(ydata)) == TypeD(ydata) | |
=# | |
function relspeed(fn, xs, ys; digits::Int=2, broadcasted::Bool=false) | |
reltime = if broadcasted | |
relspeed_broadcast_fn(fn, xs, ys) | |
else | |
relspeed_apply_fn(fn, xs, ys) | |
end | |
return round(Float64(reltime), digits=digits) | |
end | |
function relspeed(fn, x1s, x2s, y1s, y2s; digits::Int=2, broadcasted::Bool=false) | |
reltime = if broadcasted | |
relspeed_broadcast_fn(fn, x1s, x2s, y1s, y2s) | |
else | |
relspeed_apply_fn(fn, x1s, x2s, y1s, y2s) | |
end | |
return round(Float64(reltime), digits=digits) | |
end | |
function relspeed_apply_fn(fn, xs, ys) | |
xstime = @refd @belapsed ($(fn))($(xs)) | |
ystime = @refd @belapsed ($(fn))($(ys)) | |
return Float32(xstime) / Float32(ystime) | |
end | |
function relspeed_broadcast_fn(fn, xs, ys) | |
xstime = @refd @belapsed ($(fn)).($(xs)) | |
ystime = @refd @belapsed ($(fn)).($(ys)) | |
return Float32(xstime) / Float32(ystime) | |
end | |
function relspeed_apply_fn(fn, x1s, x2s, y1s, y2s) | |
xstime = @refd @belapsed ($(fn))(($(x1s)), ($(x2s))) | |
ystime = @refd @belapsed ($(fn))(($(y1s)), ($(y2s))) | |
return Float32(xstime) / Float32(ystime) | |
end | |
function relspeed_broadcast_fn(fn, x1s, x2s, y1s, y2s) | |
xstime = @refd @belapsed ($(fn)).(($(x1s)), ($(x2s))) | |
ystime = @refd @belapsed ($(fn)).(($(y1s)), ($(y2s))) | |
return Float32(xstime) / Float32(ystime) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment