empty matrix creation


#1

I am trying to fill a matrix, and currently it takes longer to create it than to fill it. Here is the code for creating it:

Array{Int64,2}(undef, 3, 10^3)

I profiled it, and it seems to spend all of its time in the Type() function in boot.jl. Am I doing something wrong here?


#2

Can you show your benchmarking code and the results?


#3

Yes, on my machine memory allocation is slower than initializing. For example,

@btime fill(0, (3,1000));
A = Array{Int,2}(undef, 3, 1000)
@btime fill!($A, 0);

gives

1.383 μs (2 allocations: 23.52 KiB)
446.586 ns (0 allocations: 0 bytes)

Maybe this isn’t an entirely fair comparison since in benchmarking the fill! call, the array A is in-cache (because @btime calls fill! over and over on the same array and reports the fastest time). On the other hand, improving cache locality is a real benefit of pre-allocating arrays.

(Obviously, if your initialization is significantly more complicated than simply setting the entries to zero, then the initialization time could become more important.)