We are thinking about the same thing ! On Fri, May 8, 2015 at 9:14 PM, Tomas Lycken <[email protected]> wrote:
> I guess the real benefit comes from being able to define f!(b, a), which > means you can save the allocation of memory for the result (by re-using > memory you have from elsewhere). For example, it could be done like this: > > function f!(b, a::Array) > n = length(a) > @assert n == length(b) > for i in 1:n > b[i] = f(a[i]) > end > b > end > > Now, calling b = f!(similar(a), a) will be equivalent to your b = f(a) in > the array case, but e.g. f!(a,a) will be faster (by a factor of 2-3 on my > machine, for this trivial function), especially for large arrays. In > idiomatic Julia, you’d probably define your f(a::Array) as f(a::Array) = > f!(similar(a), a), and expose both f and f!, to give the user the > opportunity to avoid the allocation if it suits their application. > > // T > > In this case, I think > > On Friday, May 8, 2015 at 8:55:32 PM UTC+2, Sisyphuss wrote: > > @Steven, we are not talking about the same thing. You are talking about >> vectorizing the code, while I am talking about vectorizing the function. >> >> >> On Fri, May 8, 2015 at 8:23 PM, Steven G. Johnson <[email protected]> >> wrote: >> >>> >>> >>> On Friday, May 8, 2015 at 12:30:57 PM UTC-4, Sisyphuss wrote: >>>> >>>> The following code is aimed to study the benefit of vectorization: >>>> >>> >>> (Don't assume that vectorization is beneficial. In general, vectorizing >>> your code in Julia leads to slower code, not faster code. e.g. compare x >>> = y + z + 3y + 4z^2, where y and z are vectors, to writing out the loops. >>> Vectorizing is much faster in languages like Python mainly because the >>> loops are slow.) >>> >> >> >
