Created
November 20, 2014 07:47
-
-
Save lendle/c09c50aebf6027b243c0 to your computer and use it in GitHub Desktop.
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
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) |
Author
lendle
commented
Nov 20, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment