Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to avoid allocations when multiplying numbers with missings

$
0
0

Coming from R I have a hard time to use missings and ensure type stability.

I guess that the following allocations in multiplication of numbers also relate to this issue. How can I avoid the memory allocations in the following code snipped?

x = [missing, 1.0];
y = [missing, 2.0];
f(x,y) = x[1] * y[1] * x[2] * y[2];
@time f(x,y); @time f(x,y)

  0.000006 seconds (2 allocations: 32 bytes)

Please, help me so that I can write own scalar operations within loops without allocating memory in each iteration. (Do not focus on special solutions for the exact formula in the example.) E.g. in the following snippet there are allocations in each iteration.

function f(x,y)
    s = zero(eltype(x))
    for i in axes(x,1), j in axes(y,1)
        sij = x[i] * x[j] + y[i] * y[j]
        if !ismissing(sij) 
            s += sij
        end
    end
    s
end

In the example I could check for any missings before the formula. However, that makes more complex code even more complex and it does still allocate when using any on a tuple.

function f(x,y)
    T = nonmissingtype(eltype(x))
    s = zero(T)
    for i in axes(x,1), j in axes(y,1)
        #if !any(ismissing.((x[i], x[j], y[i], y[j])))
        if !ismissing(x[i]) && !ismissing(x[j]) && !ismissing(y[i]) && !ismissing(y[i]) 
            sij = x[i] * x[j] + y[i] * y[j]
            s += sij::T
        end
    end
    s
end
@time f(x,y); @time f(x,y)
0.000004 seconds (1 allocation: 16 bytes)

I guess there is a simpler and more general way of dealing with missings.

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles