Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmarks for map and broadcast with Union{T, Missing} arrays #176

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions src/union/UnionBenchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ using Compat

const SUITE = BenchmarkGroup()

########################
###########################
# array Union{T, Nothing} #
########################
###########################

g = addgroup!(SUITE, "array")

Expand All @@ -19,6 +19,14 @@ const VEC_LENGTH = 1000
_zero(::Type{T}) where {T} = zero(T)
_zero(::Type{Union{T, Nothing}}) where {T} = zero(T)

_abs(x) = abs(x)
_abs(::Nothing) = nothing

_mul(x, y) = x * y
_mul(::Nothing, ::Any) = nothing
_mul(::Any, ::Nothing) = nothing
_mul(::Nothing, ::Nothing) = nothing

function perf_sum(X::AbstractArray{T}) where T
s = _zero(T) + _zero(T)
@inbounds @simd for x in X
Expand All @@ -43,6 +51,22 @@ function perf_countequals(X::AbstractArray, Y::AbstractArray)
n
end

function perf_simplecopy(X::AbstractArray)
ret = similar(X)
@inbounds for i in eachindex(X)
ret[i] = X[i]
end
ret
end

function perf_binaryop(op::Function, X::AbstractArray, Y::AbstractArray)
ret = similar(X, Union{Nothing, typeof(op(_zero(eltype(X)), _zero(eltype(Y))))})
@inbounds for i in eachindex(X, Y)
ret[i] = op(X[i], Y[i])
end
ret
end

for T in (Bool, Int8, Int64, Float32, Float64, BigInt, BigFloat, Complex{Float64})
if T == BigInt
S = Int128
Expand All @@ -60,14 +84,35 @@ for T in (Bool, Int8, Int64, Float32, Float64, BigInt, BigFloat, Complex{Float64
X2[samerand(VEC_LENGTH) .> .9] = nothing
Y2[samerand(VEC_LENGTH) .> .9] = nothing

for A in (X, X2)
g["perf_sum", string(typeof(A))] = @benchmarkable perf_sum($A)
g["perf_countnothing", string(typeof(A))] = @benchmarkable perf_countnothing($A)
for (M, A) in ((false, X), (true, X2))
g["perf_sum", T, M] = @benchmarkable perf_sum($A)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how the benchmarks names work: is it OK to use three dimensions and to use values directly rather than strings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know. @jrevels?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can use arbitrary tuples of values for benchmark keys

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main guideline is to keep them relatively short - long keys are formatted annoyingly in benchmark reports

g["perf_countnothing", T, M] = @benchmarkable perf_countnothing($A)

g["perf_simplecopy", T, M] =
@benchmarkable perf_simplecopy($A)
g["map", identity, T, M] =
@benchmarkable map(identity, $A)
g["broadcast", identity, T, M] =
@benchmarkable broadcast(identity, $A)

g["map", abs, T, M] =
@benchmarkable map(_abs, $A)
g["broadcast", abs, T, M] =
@benchmarkable broadcast(_abs, $A)
end

for (A, B) in ((X, Y), (X2, Y2), (X, Y2))
g["perf_countequals", string(eltype(A), eltype(B))] =
for (M, A, B) in (((false, false), X, Y),
((true, true), X2, Y2),
((false, true), X, Y2))
g["perf_countequals", string(T)] =
@benchmarkable perf_countequals($A, $B)

g["perf_binaryop", *, T, M] =
@benchmarkable perf_binaryop(_mul, $A, $B)
g["map", *, T, M] =
@benchmarkable map(_mul, $A, $B)
g["broadcast", *, T, M] =
@benchmarkable broadcast(_mul, $A, $B)
end
end

Expand Down