Skip to content

Instantly share code, notes, and snippets.

@lendle
Created November 20, 2014 07:47
Show Gist options
  • Save lendle/c09c50aebf6027b243c0 to your computer and use it in GitHub Desktop.
Save lendle/c09c50aebf6027b243c0 to your computer and use it in GitHub Desktop.
immutable NullableVector{T}
isnull::Vector{Uint8}
values::Vector{T}
end
Base.length(nv::NullableVector) = length(nv.isnull)
function Base.getindex(nv::NullableVector{Float64}, i::Integer)
return bool(nv.isnull[i]) ? Nullable{Float64}() : Nullable{Float64}(nv.values[i])
end
function f1(nv::NullableVector{Float64})
s, n = 0.0, 0
for i in 1:length(nv)
nv_i = nv[i]
if !bool(isnull(nv_i))
s += get(nv_i)
n += 1
end
end
return s / n
end
function f2(nv::NullableVector{Float64})
s, n = 0.0, 0
for i in 1:length(nv)
if !isnull(nv[i])
s += get(nv[i])
n += 1
end
end
return s / n
end
function g1(v::Vector{Float64})
s, n = 0.0, 0
for i in 1:length(v)
v_i = v[i]
if !isnan(v_i)
s += v_i
n += 1
end
end
return s / n
end
function g2(v::Vector{Float64})
s, n = 0.0, 0
for i in 1:length(v)
if !isnan(v[i])
s += v[i]
n += 1
end
end
return s / n
end
function h1(v::Vector{Nullable{Float64}})
s, n = 0.0, 0
for i in 1:length(v)
v_i = v[i]
if !isnull(v_i)
s += get(v_i)
n += 1
end
end
return s / n
end
function h2(v::Vector{Nullable{Float64}})
s, n = 0.0, 0
for i in 1:length(v)
if !isnull(v[i])
s += get(v[i])
n += 1
end
end
return s / n
end
const N = 50_000_000
v = randn(N)
nv = NullableVector(zeros(Uint8, N), copy(v))
vn = Nullable{Float64}[Nullable{Float64}(x) for x in v]
@printf("NullableVector{Float64} w/ One GetIndex Call\n")
f1(nv)
@time f1(nv)
@time f1(nv)
@time f1(nv)
@printf("NullableVector{Float64} w/ Two GetIndex Calls\n")
f2(nv)
@time f2(nv)
@time f2(nv)
@time f2(nv)
@printf("Vector{Float64} w/ One GetIndex Call\n")
g1(v)
@time g1(v)
@time g1(v)
@time g1(v)
@printf("Vector{Float64} w/ Two GetIndex Calls\n")
g2(v)
@time g2(v)
@time g2(v)
@time g2(v)
@printf("Vector{Nullable{Float64}} w/ One GetIndex Call\n")
h1(vn)
@time h1(vn)
@time h1(vn)
@time h1(vn)
@printf("Vector{Nullable{Float64}} w/ Two GetIndex Calls\n")
h2(vn)
@time h2(vn)
@time h2(vn)
@time h2(vn)
@lendle
Copy link
Author

lendle commented Nov 20, 2014

NullableVector{Float64} w/ One GetIndex Call
elapsed time: 0.067090492 seconds (13824 bytes allocated)
elapsed time: 0.067294714 seconds (96 bytes allocated)
elapsed time: 0.067350359 seconds (96 bytes allocated)
NullableVector{Float64} w/ Two GetIndex Calls
elapsed time: 0.109128652 seconds (96 bytes allocated)
elapsed time: 0.109454591 seconds (96 bytes allocated)
elapsed time: 0.110037043 seconds (96 bytes allocated)
Vector{Float64} w/ One GetIndex Call
elapsed time: 0.040197333 seconds (96 bytes allocated)
elapsed time: 0.040698076 seconds (96 bytes allocated)
elapsed time: 0.040394534 seconds (96 bytes allocated)
Vector{Float64} w/ Two GetIndex Calls
elapsed time: 0.040077746 seconds (96 bytes allocated)
elapsed time: 0.040728007 seconds (96 bytes allocated)
elapsed time: 0.040349113 seconds (96 bytes allocated)
Vector{Nullable{Float64}} w/ One GetIndex Call
elapsed time: 0.050442219 seconds (96 bytes allocated)
elapsed time: 0.051303285 seconds (96 bytes allocated)
elapsed time: 0.051102903 seconds (96 bytes allocated)
Vector{Nullable{Float64}} w/ Two GetIndex Calls
elapsed time: 0.058091949 seconds (96 bytes allocated)
elapsed time: 0.058662928 seconds (96 bytes allocated)
elapsed time: 0.058782579 seconds (96 bytes allocated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment