The reason for the slowdown of case 4 is that Julia is not good at
inferring the return types of functions passed as arguments to other
functions.  This shows up in a few places, for instance the `map`
function has this problem too which I think has been partly hacked
around.  This is a bit of a bummer as higher-order functions are a nice
thing to have but slow in Julia.  I had a quick go at trying to find
relevant issues on github but no luck.

Others will be able to be more specific.

On Wed, 2015-01-07 at 17:21, Nils Gudat <[email protected]> wrote:
> 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?

Reply via email to