Created
August 22, 2013 01:48
-
-
Save binarybana/6302335 to your computer and use it in GitHub Desktop.
Immutable vs Composite types in Julia
This file contains hidden or 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
module TestMod | |
immutable ITest | |
x :: Float64 | |
y :: Float64 | |
z :: Vector{Float64} | |
end | |
type CTest | |
x :: Float64 | |
y :: Float64 | |
z :: Vector{Float64} | |
end | |
function memcpy!(dest::CTest, src::CTest) | |
dest.x = src.x | |
dest.y = src.y | |
for i=1:length(src.z) | |
dest.z[i] = src.z[i] | |
end | |
end | |
function testi(N::Int) | |
curr = ITest(0.0, 0.0, zeros(5)) | |
old = curr | |
temparray = zeros(5) | |
for i=1:N | |
for i in 1:5 | |
temparray[i] = randn() + curr.z[i] | |
end | |
curr = ITest(curr.x + randn(), curr.y + randn(), temparray) | |
#curr = ITest(curr.x + randn(), curr.y + randn(), curr.z + randn(5)) | |
if rand() < 0.5 | |
old = curr # Accept | |
else | |
curr = old # Reject | |
end | |
end | |
return None | |
end | |
function testc(N::Int) | |
curr = CTest(0.0, 0.0, zeros(5)) | |
old = deepcopy(curr) | |
for i=1:N | |
curr.x += randn() | |
curr.y += randn() | |
#curr.z += randn(5) | |
for j=1:5 | |
curr.z[j] += randn() | |
end | |
if rand() < 0.5 | |
memcpy!(old, curr) # Accept | |
else | |
memcpy!(curr,old) # reject | |
end | |
end | |
return None | |
end | |
N=10 | |
testi(N) | |
testc(N) | |
N = 10^6 | |
#gc_disable() | |
println("testi: $(@time testi(N))") | |
println("testc: $(@time testc(N))") | |
#@profile testi(10^5) | |
#Profile.print() | |
#Profile.clear() | |
#gc_enable() | |
end |
This file contains hidden or 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
julia> include("mcmcspeed.jl") | |
elapsed time: 0.293128759 seconds (32000272 bytes allocated) | |
elapsed time: 0.254959104 seconds (800 bytes allocated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will cost you 5-10% of a second per 10^6 iterations, but I think it's about as simple as you're going to get.