Skip to content

Instantly share code, notes, and snippets.

@nalimilan
Last active June 18, 2016 21:34
Show Gist options
  • Save nalimilan/94a3dc790bc592e8b2b561d9dcca9a9b to your computer and use it in GitHub Desktop.
Save nalimilan/94a3dc790bc592e8b2b561d9dcca9a9b to your computer and use it in GitHub Desktop.
using NullableArrays
@inline function f1{S1, S2}(x::Nullable{S1}, y::Nullable{S2})
if isbits(S1) & isbits(S2)
Nullable{Base.promote_op(+, S1, S2)}(x.value + y.value, x.isnull | y.isnull)
else
error()
end
end
@inline function f2{S1, S2}(x::Nullable{S1}, y::Nullable{S2})
if isbits(S1) & isbits(S2)
Nullable{Base.promote_op(+, S1, S2)}(x.value + y.value, x.isnull | y.isnull)
else
x.isnull | y.isnull ? Nullable{Base.promote_op(+, S1, S2)}() :
Nullable{Base.promote_op(+, S1, S2)}(x.value + y.value)
end
end
@inline function f3{S1, S2}(x::Nullable{S1}, y::Nullable{S2})
if x.isnull | y.isnull
Nullable{Base.promote_op(+, S1, S2)}()
else
Nullable{Base.promote_op(+, S1, S2)}(x.value + y.value)
end
end
# Fastest version we can get by relying on NullableArray implementation details
# NOTE: it's actually the slowest one!
function testref(x::NullableVector, y::NullableVector)
@inbounds for i in 1:length(x)
x.values[i] = x.values[i] + y.values[i]
x.isnull[i] = x.isnull[i] | y.isnull[i]
end
x
end
function testf1(x::AbstractVector, y::AbstractVector)
@inbounds for i in 1:length(x)
x[i] = f1(x[i], y[i])
end
x
end
function testf2(x::AbstractVector, y::AbstractVector)
@inbounds for i in 1:length(x)
x[i] = f2(x[i], y[i])
end
x
end
function testf3(x::AbstractVector, y::AbstractVector)
@inbounds for i in 1:length(x)
x[i] = f3(x[i], y[i])
end
x
end
x = NullableArray(rand(10000000), rand(Bool, 10000000));
y = NullableArray(rand(10000000), rand(Bool, 10000000));
using BenchmarkTools
mean((@benchmark testref(x, y)).times)
mean((@benchmark testf1(x, y)).times)
mean((@benchmark testf2(x, y)).times)
mean((@benchmark testf3(x, y)).times)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment