In Python, it is usually suggested to vectorize the code to make computation faster. For example, if you want to compute the inner product of two vectors, say a
and b
, usually
Code A
c = np.dot(a, b)
is better than
Code B
c = 0
for i in range(len(a)):
c += a[i] * b[i]
But in Julia, it seems sometimes vectorization is not that helpful. I reckoned '*
and dot
as vectorized versions and an explicit for
loop as a non-vectorized version and got the following result.
using Random
using LinearAlgebra
len = 1000000
rng1 = MersenneTwister(1234)
a = rand(rng1, len)
rng2 = MersenneTwister(12345)
b = rand(rng2, len)
function inner_product(a, b)
result = 0
for i in 1: length(a)
result += a[i] * b[i]
end
return result
end
@time a' * b
@time dot(a, b)
@time inner_product(a, b);
0.013442 seconds (56.76 k allocations: 3.167 MiB)
0.003484 seconds (106 allocations: 6.688 KiB)
0.008489 seconds (17.52 k allocations: 976.752 KiB)
(I know using BenchmarkTools.jl
is a more standard way to measure the performance.)
From the output, dot
runs faster than for
than '*
, which is a contradiction to what has been presumed.
So my question is,
- does Julia need (or sometimes need) vectorization to speed up computation?
- If it does, then when to use vectorization and which is the better way to use (consider
dot
and'*
)? - If it does not, then what is the difference between Julia and Python in terms of the mechanism of vectorized and non-vectorized codes?