Consider the multiplication of a n-by-n matrix M and a n-dimensional vector v, M*v. One may write v either
- as an n-element Array (
typeof(v) <: AbstractVectoristrue), - or write it as an
n×1array (typeof(v) <: AbstractVectorisfalse).
Glancing through the source code matmul.jl, it looks like one uses gemv! and the other uses gemm_wrapper!; as for timing, there seems to be very small differences
m=rand(10000,10000);
v=rand(10000);
v2=rand(10000,1);
@btime $m*$v;
34.902 ms (2 allocations: 78.20 KiB)
@btime $m*$v2;
36.459 ms (2 allocations: 78.20 KiB)
Also, to make an n-by-n matrix from v*v', @which shows Vector uses adjtrans.jl while n-by-1 array uses matmul.jl. Looks like AbstractVector can be slightly faster for high dimension cases, and for things like v'*m*v, typeof(v) <: AbstractVector gives a number whereas n-by-1 matrix v gives a 1-by-1 matrix.
Generally, what’s the pros/cons of using one as opposed to the other, and is there any preferences for using either one?
Thanks!