Yes, making the function-argument version just as fast is high on my to-do list.
On Wed, Apr 3, 2013 at 7:24 PM, Colm Ryan <
co...@colmryan.org> wrote:
> I'm writing a generic cumulative reduce function and the performance seems
> to be about ten time slower than the built-in ones, for example cumsum. It
> seems to boil down to the JIT not being able to accelerate a passed
> function. The following example which compares passing and not passing a
> function handle sums up the issue:
>
> function reducetest(op::Function, v::AbstractVector)
> n = length(v)
> out = similar(v, typeof((zero(eltype(v)))))
> out[1] = v[1]
> for ct = 2:n
> out[ct] = op(v[ct],out[ct-1])
> end
> return out
> end
>
> function reducetest(v::AbstractVector)
> n = length(v)
> out = similar(v, typeof((zero(eltype(v)))))
> out[1] = v[1]
> for ct = 2:n
> out[ct] = +(v[ct],out[ct-1])
> end
> return out
> end
>
> function speedtest(arraySize)
> junk = randn(arraySize)
> print("\nBuiltin cumsum:\n")
> @time (for ct = 1:100, out = cumsum(junk); end)
> print("\nPasssing function:\n")
> @time (for ct = 1:100, out = reducetest(+, junk); end)
> print("\nNot passsing function:\n")
> @time (for ct = 1:100, out = reducetest(junk); end)
>
>
> end
>
>
> which gives:
>
> julia>speedtest(100000)
>
> Builtin cumsum:
> elapsed time: 0.082851409 seconds
> Passsing function:
> elapsed time: 1.065588846 seconds
> Not passsing function:
> elapsed time: 0.069623861 seconds
>
>
> I also noticed the reduce function check the passed function and specializes
> for the addition and multiplication operators, presumably for the same
> performance reasons. I was curious if there is any hope of making the
> general passed function version just as fast?
>
> --Colm
>
>