BTW, it does work if you put it into a function rather than running it in 
global scope:
julia> test(y) = map(x->10^x, y)
test (generic function with 1 method)

julia> @time test(a);
  0.003387 seconds (4.34 k allocations: 97.662 KB)

julia> @time test(a);
  0.000440 seconds (4.01 k allocations: 78.422 KB)

This is related 
to 
http://docs.julialang.org/en/release-0.4/manual/performance-tips/#avoid-global-variables



On Tuesday, May 10, 2016 at 9:03:26 PM UTC+2, Yichao Yu wrote:
>
> On Tue, May 10, 2016 at 2:22 PM,  <[email protected] <javascript:>> 
> wrote: 
> > Watch this. 
> > 
> > We define an array: 
> > a = collect(-1:0.001:1) 
> > 
> > Then map something over the aforementioned array for the first time: 
> > @time map(x->10^x, a) 
> > # 0.049258 seconds 
> > 
> > Do it again so that we don't capture JIT compilation time: 
>
> This doesn't work 
>
> > @time map(x->10^x, a) 
>
> Since this is a different anonymous function. 
>
> > # 0.048793 seconds -- didn't change much 
> > 
> > Now we assign the anonymous function to a variable: 
> > f = x->10^x 
> > 
> > Then we map the variable over the array: 
> > @time map(f, a) 
> > # 0.047853 seconds 
> > 
> > Again so that we avoid JIT: 
>
> It works this time 
>
> > @time map(f, a) 
>
> Since this is the same anonymous function 
>
> > # 0.000386 seconds -- wow 
> > 
> > What is happening here? 
> > Why does assigning an anonymous function to a variable makes the 
> function 
> > execution so much faster? 
> > 
> > 
> > Yousef 
>

Reply via email to