This compilation cost is proportional to the amount of code you type into the REPL or editor, which is modest and very limited (unless you're generating code). I understand why it might be of interest and worth noting, but is there an actual problem? 50 milliseconds is well below the threshold of what is considered perceptually instantaneous.
On Wed, May 11, 2016 at 2:00 PM, Yichao Yu <[email protected]> wrote: > On Wed, May 11, 2016 at 5:13 AM, Andras Niedermayer > <[email protected]> wrote: > > 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 > > No it's not. > The use of global variable only affect top level code (i.e. the call > of `map`) which basically takes no time at all. > It works for this case for the same reason I mentioned above. The > lambda used in both cases are the same one so no recompilation is > needed. > > > > > > > > > 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]> 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 >
