The first time you run a function with a certain tuple of types of
arguments it gets just-in-time compiled adding to the time you measure.
Subsequent runs will be faster and allocate much less memory. Try:

fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@time(fib(20))
@time(fib(20))

and consider the second result.


On Fri, Sep 18, 2015 at 1:23 AM, Frank Kampas <[email protected]> wrote:

> On julialang.org, it is stated that Julia is about 170 times faster than
> Mathematica for the the test "fib".  I get about a factor 2.
>
> Julia:
>
> fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
> println(@time(fib(20)))
>
> elapsed time: 0.001868071 seconds (33280 bytes allocated)
> 6765
>
>
> Mathematica:
>
>
> In[1]:= AbsoluteTiming[fib = 
> Compile[{{n,_Integer}},If[n<2,n,fib[n-1]+fib[n-2]]]]
> Out[1]= {0.000134563,CompiledFunction[Argument count: 1
> Argument types: {_Integer}]}
>
>
>
> In[2]:= AbsoluteTiming[fib[20]]
> Out[2]= {0.00385017,6765}
>
>
>
>
>

Reply via email to