Skip to content

Instantly share code, notes, and snippets.

@ssfrr
Last active August 29, 2015 14:03
Show Gist options
  • Save ssfrr/b4885e3fc10ea3f2ab9d to your computer and use it in GitHub Desktop.
Save ssfrr/b4885e3fc10ea3f2ab9d to your computer and use it in GitHub Desktop.
Testing iterating through objects at global scope
function for_in!(arr::Array{Float32})
l::Int = length(arr)
for i::Int in 1:l
arr[i] += 2pi
end
gc()
end
function while_loop!(arr::Array{Float32})
i::Int = 1
l::Int = length(arr)
while i <= l
arr[i] += 2pi
i += 1
end
gc()
end
function run_test()
println("\nTesting at function scope")
println("-------------------------")
arr = Array(Float32, 1024)
# run once for jitting
for_in!(arr)
while_loop!(arr)
println("Testing For Iteration")
gc()
@time for_in!(arr)
println("@allocated: ", @allocated for_in!(arr))
println("Testing While Iteration")
gc()
@time while_loop!(arr)
println("@allocated: ", @allocated while_loop!(arr))
end
println("\nTesting at global scope")
println("-----------------------")
arr = Array(Float32, 1024)
# run once for jitting
for_in!(arr)
while_loop!(arr)
println("Testing For Iteration")
gc()
@time for_in!(arr)
println("@allocated: ", @allocated for_in!(arr))
println("Testing While Iteration")
gc()
@time while_loop!(arr)
println("@allocated: ", @allocated while_loop!(arr))
run_test()
# Testing at global scope
# -----------------------
# Testing For Iteration
# elapsed time: 0.012072643 seconds (13856 bytes allocated, 90.73% gc time)
# @allocated: 0
# Testing While Iteration
# elapsed time: 0.010270464 seconds (80 bytes allocated, 99.85% gc time)
# @allocated: 0
#
# Testing at function scope
# -------------------------
# Testing For Iteration
# elapsed time: 0.010121247 seconds (0 bytes allocated, 99.95% gc time)
# @allocated: 0
# Testing While Iteration
# elapsed time: 0.009629785 seconds (0 bytes allocated, 99.95% gc time)
# @allocated: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment