@ChrisRackauckas
I benchmarked a simple loop of 1,000,000 iterations using both push! and preallocation, both using functions and within the global space, and using const versus non-const arrays when executing in the global space. I got some unexpected results, which perhaps somebody can explain.
using BenchmarkTools
function alloc_push()
begin
a = []
for i in 1:1000000
push!(a, i)
end
end
end
begin # requires an indent in juno
println("-----------"); for i in 1:5
@time alloc_push()
end;
println("Execution of function alloc_push()")
println("Cost is about 0.03 sec, with 1M allocs of total 24MBytes")
println("----------")
end
const bconst = zeros(Int, 1000000)
bnonconst = zeros(Int, 1000000)
function alloc_pre(b)
begin
for i in 1:1000000
b[i] = i
end
end
end
# No allocations whether b
begin
println("-----");
println("....const")
for j in 1:5 @time alloc_pre(bconst); end
println("...nonconst")
for j in 1:5 @time alloc_pre(bnonconst); end
end; println("function call to alloc_pre(): memory is preallocated")
println("If argument is const, time is 0.002 sec")
println("If argument is non-const, time is 0.0002 sec")
println("If I increase array size by 10x, all timings increase by 10x")
println("WHY IS CONST slower?")
println("-----")
end
# 2M eallocationsif `b` is not a const
begin
println("------");
println("...const")
for j in 1:5
@time for i in 1:1000000 bconst[i] = i; end
end
println("...nonconst")
for j in 1:5
@time for i in 1:1000000 bnonconst[i] = i; end
end
println("Running in global space, 0.002 sec with const array, with no allocations")
println(" 0.04 sec with non-const array, with 2M allocations of total 30.5MBytes with gc")
println("Execution outside function in global space: ")
println(" const is 10x faster as expected")
end;
To summarize (also contained within the code):
- When executing in the global space, with pre-allocated arrays, using const arrays executes without allocations and 10x faster than the non-const version. That is what I expected.
- When executing the loops within a function with the array as an argument, and this array can be const or non-const, the results are stranger: whether I use a non-const or a const array, I get no memory allocation. Moreover, using a const array runs 10x slower than using a non-const array. Why would that be?
I am running on a Macbook Pro notebook, on the CPU, with 16 MB of RAM, so memory is not the issue. Can anybody duplicate these results? My goal is to gain a better understanding of when memory is allocated, what I can control and what I cannot, and how to use these findings in code where speed is important.
Thanks.
13 posts - 4 participants