Created
July 2, 2015 16:41
-
-
Save davidagold/e683cc5c41e593246f43 to your computer and use it in GitHub Desktop.
Reduce methods testing/profiling
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
using NullableArrays | |
A = rand(5_000_000) | |
X = NullableArray(A) | |
f(x) = 5 * x | |
f{T<:Number}(x::Nullable{T}) = Nullable(5 * x.value, x.isnull) | |
function Base.mapreduce_impl{T}(f, op::Base.MinFun, X::NullableArray{T}, first::Int, last::Int) | |
# locate the first non-null entry | |
i = first | |
while X.isnull[i] && i <= last | |
i += 1 | |
end | |
@inbounds v = f(X[i]) | |
i += 1 | |
# find min | |
while i <= last | |
if !X.isnull[i] | |
@inbounds x = f(X[i]) | |
if (x < v).value | |
v = x | |
end | |
end | |
i += 1 | |
end | |
return v | |
end | |
function Base.mapreduce_impl{T}(f, op::Base.MaxFun, X::NullableArray{T}, first::Int, last::Int) | |
# locate the first non-null entry | |
i = first | |
while X.isnull[i] && i <= last | |
i += 1 | |
end | |
@inbounds v = f(X[i]) | |
i += 1 | |
# find min | |
while i <= last | |
if !X.isnull[i] | |
@inbounds x = f(X[i]) | |
if (x > v).value | |
v = x | |
end | |
end | |
i += 1 | |
end | |
return v | |
end | |
function Base.abs{T}(x::Nullable{T}) | |
if isbits(T) | |
return Nullable(abs(x.value), x.isnull) | |
else | |
throw_error() | |
end | |
end | |
function Base.abs2{T}(x::Nullable{T}) | |
if isbits(T) | |
return Nullable(abs2(x.value), x.isnull) | |
else | |
throw_error() | |
end | |
end | |
function test_mapreduce(A::AbstractArray, X::NullableArray) | |
mapreduce(f, Base.(:+), A) | |
mapreduce(f, Base.(:+), X) | |
println("Method: mapreduce(f, op, A)") | |
print(" for Array{Float64}: ") | |
@time(mapreduce(f, Base.(:+), A)) | |
print(" for NullableArray{Float64}: ") | |
@time(mapreduce(f, Base.(:+), X)) | |
end | |
function test_reduce(A::AbstractArray, X::NullableArray) | |
reduce(Base.(:+), A) | |
reduce(Base.(:+), X) | |
println("Method: reduce(op, A)") | |
print(" for Array{Float64}: ") | |
@time(reduce(Base.(:+), A)) | |
print(" for NullableArray{Float64}: ") | |
@time(reduce(Base.(:+), X)) | |
end | |
function test_sum1(A::AbstractArray, X::NullableArray) | |
sum(A) | |
sum(X) | |
println("Method: sum(A)") | |
print(" for Array{Float64}: ") | |
@time(sum(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(sum(X)) | |
end | |
function test_sum2(A::AbstractArray, X::NullableArray) | |
sum(f, A) | |
sum(f, X) | |
println("Method: sum(f, A)") | |
print(" for Array{Float64}: ") | |
@time(sum(f, A)) | |
print(" for NullableArray{Float64}: ") | |
@time(sum(f, X)) | |
end | |
function test_prod1(A::AbstractArray, X::NullableArray) | |
prod(A) | |
prod(X) | |
println("Method: prod(A)") | |
print(" for Array{Float64}: ") | |
@time(prod(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(prod(X)) | |
end | |
function test_prod2(A::AbstractArray, X::NullableArray) | |
prod(f, A) | |
prod(f, X) | |
println("Method: prod(f, A)") | |
print(" for Array{Float64}: ") | |
@time(prod(f, A)) | |
print(" for NullableArray{Float64}: ") | |
@time(prod(f, X)) | |
end | |
function test_minimum1(A::AbstractArray, X::NullableArray) | |
minimum(A) | |
minimum(X) | |
println("Method: minimum(A)") | |
print(" for Array{Float64}: ") | |
@time(minimum(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(minimum(X)) | |
end | |
function test_minimum2(A::AbstractArray, X::NullableArray) | |
minimum(f, A) | |
minimum(f, X) | |
println("Method: minimum(f, A)") | |
print(" for Array{Float64}: ") | |
@time(minimum(f, A)) | |
print(" for NullableArray{Float64}: ") | |
@time(minimum(f, X)) | |
end | |
function test_maximum1(A::AbstractArray, X::NullableArray) | |
maximum(A) | |
maximum(X) | |
println("Method: maximum(A)") | |
print(" for Array{Float64}: ") | |
@time(maximum(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(maximum(X)) | |
end | |
function test_maximum2(A::AbstractArray, X::NullableArray) | |
maximum(f, A) | |
maximum(f, X) | |
println("Method: maximum(f, A)") | |
print(" for Array{Float64}: ") | |
@time(maximum(f, A)) | |
print(" for NullableArray{Float64}: ") | |
@time(maximum(f, X)) | |
end | |
function test_sumabs(A::AbstractArray, X::NullableArray) | |
sumabs(A) | |
sumabs(X) | |
println("Method: sumabs(A)") | |
print(" for Array{Float64}: ") | |
@time(sumabs(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(sumabs(X)) | |
end | |
function test_sumabs2(A::AbstractArray, X::NullableArray) | |
sumabs2(A) | |
sumabs2(X) | |
println("Method: sumabs2(A)") | |
print(" for Array{Float64}: ") | |
@time(sumabs2(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(sumabs2(X)) | |
end | |
function test_mean1(A::AbstractArray, X::NullableArray) | |
mean(A) | |
mean(X) | |
println("Method: mean(A)") | |
print(" for Array{Float64}: ") | |
@time(mean(A)) | |
print(" for NullableArray{Float64}: ") | |
@time(mean(X)) | |
end | |
function testall() | |
test_mapreduce(A, X) | |
test_reduce(A, X) | |
test_sum1(A, X) | |
test_sum2(A, X) | |
test_prod1(A, X) | |
test_prod2(A, X) | |
test_minimum1(A, X) | |
test_minimum2(A, X) | |
test_maximum1(A, X) | |
test_maximum2(A, X) | |
test_sumabs(A, X) | |
test_sumabs2(A, X) | |
return nothing | |
end | |
# test_mean1(A, X) # <-- MethodError: `/` has no method matching /(::Nullable{Float64}, ::Int64) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment