tryingtosolve
tryingtosolve

Reputation: 803

Julia - why does including variables in a module take up so much memory?

Consider these two functions:

Function 1:

function testVec(t,v,k,n)
    for i=1:n
        t .= v .* 3 .+ k .* 4;
    end
end

Function 2:

module params
r = 3;
end

function testVec2(t,v,k,n)
    for i=1:n
        t .= v .* params.r .+ k .* 4;
    end
end

They have drastically different performance:

@time testVec([1 2 3 4], [2 3 4 5], [3 4 5 6], 1000)
0.000036 seconds (7 allocations: 496 bytes)

@time testVec2([1 2 3 4], [2 3 4 5], [3 4 5 6], 1000)
0.003180 seconds (4.01 k allocations: 141.109 KiB)

Why does including the parameter r in a module make the function perform worse?

If I export the module params and include r in testVec2 without using the prefix params, its performance immediately improves (same as testVec). Why?

Upvotes: 1

Views: 84

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

r in params module is a non-const global, which makes its type unstable (because some function can assign r to something else, of a different type).

Replace r = 3 with const r = 3 and the timings will be the same. Also see first section of Performance Tips.

Upvotes: 3

Related Questions