I'm trying to understand the performance gains from supplying type
information to functions. To this end, I've written a short piece of code
<https://github.com/nilshg/LearningModels/blob/master/test_argument_passing.jl>
that takes a vector x of the integers from 1:100000 and multiplies it with
a vector y containing the integers from 1:10. I've written this in a loop
and am storing the results in a 100,000-by-10 matrix.
I then write the function that contains the loop that multiplies the number
pairs and assigns the results to the matrix in four different ways:
1. Without arguments
2. Passing results::Array{Float64, 2} as argument
3. Passing results::Array{Float64, 2}, x::Array{Float64, 1} and
y::Array{Float64, 1} as arguments
4. Passing results, x, y and f::Function (which multiplies x and y)
As expected, the first way is slower than the others and allocates much
more memory. Just passing the results array does not make a difference,
while passing both results and x and y reduces the computation time by a
factor of around 200 and the memory allocation by a factor of around. 1.5
million.
However, when I pass the function as an argument as well, the computation
time goes up to about a third of the function without arguments, while the
memory allocation goes up to around half.
Can anyone explain this? How can passing the function (i.e. providing more
information to the compiler) lead to such a dramatic loss in performance?