Looking for simple example to explain Ahead of Time Compilation

In Julia 1.3, this used to work a charm:

julia> add(x, y) = x + y
add (generic function with 1 method)

julia> @time add(1, 3)
  0.002479 seconds (757 allocations: 45.251 KiB)
4

julia> @time add(2, 4)
  0.000002 seconds (4 allocations: 160 bytes)
6

julia> @time add(2 + im, 3)
  0.011081 seconds (16.43 k allocations: 946.883 KiB)
5 + 1im

But now Julia is too clever, and in ways I don’t actually understand, and I’m struggling to cook up a simple example for complete beginners that is this effective. (The old favourite mandel.jl, which is not simple but cool, is also not complicated enough in Julia 1.6 to demonstrate JIT).

1 Like

This is because @time macro forces compilation before timing starts. You can try directly calling time().

julia> t = time(); add(1,2); time() - t
0.006040096282958984

julia> t = time(); add(1,2); time() - t
0.00031280517578125

julia> t = time(); add(1,2+im); time() - t
0.13146090507507324

julia> t = time(); add(1,2+im); time() - t
0.00032591819763183594
3 Likes

Ahh!! So, that was the change. Thanks!

1 Like

note that @time eval(:(add(1,2+im)) will still do what you want.

3 Likes

Or the easier to write

julia> @time @eval add(1, 2+im)
5 Likes

Perfect, thanks

Just use vector inputs.

julia> add(x,y) = x+y
add (generic function with 1 method)

julia> x = [1.2, 3.4, 5.6];

julia> y = [0.4, 0.7, 0.9];

julia> @time add(x,y);
  0.126445 seconds (273.29 k allocations: 14.544 MiB, 99.96% compilation time)

julia> @time add(x,y);
  0.000011 seconds (1 allocation: 80 bytes)

This is on Julia 1.7.0-rc2.

1 Like