Hi,
I am trying to do x’*y vector multiplication. Then I have the function
function vectorMvector_Int8(x::Array{Int8,1}, y::Array{Int8,1})
result = x'*y
result
end
I have another version that using for loop to do the multiplication
function vectorMvector2_Int8(x::Array{Int8,1}, y::Array{Int8,1})
result=0
n = size(x)[1]
@inbounds @simd for i in 1:n
result += x[i]*y[i]
end
result
end
When the vectors are small, vector multiplication are slower than for loop
e.g. a=rand([Int8(0),Int8(1)], 100);b=rand([Int8(0),Int8(1)], 100)
benchmark median for vectorMvector is 75 ns while vectorMvector2 is 29 ns.
When vectors are large, for example a=rand([Int8(0),Int8(1)], 100000);b=rand([Int8(0),Int8(1)], 100000), benchmark median for vectorMvector is 11 μs while vectorMvector2 is 24 μs.
However when x,y is Int64 type, vectorMvector2 seems always a little bit faster than vectorMvector
function vectorMvector_Int64(x::Array{Int64,1}, y::Array{Int64,1})
result = x'*y
result
end
function vectorMvector2_Int64(x::Array{Int64,1}, y::Array{Int64,1})
result=0
n = size(x)[1]
@inbounds @simd for i in 1:n
result += x[i]*y[i]
end
result
end
a=rand([Int64(0),Int64(1)], 100);b=rand([Int64(0),Int64(1)], 100), 31 ns vs 28 ns.
a=rand([Int64(0),Int64(1)], 100000);b=rand([Int64(0),Int64(1)], 100000) 31.734 μs vs 28.916 μs
I prefer to use x’*y because it is more meaningful and less typing. But the performance is complicating my choices. Am I doing something wrong for the x’*y operation to cause slow performance?
Thank you!