# Looking for simple example to explain Ahead of Time Compilation

**URL:** <https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471>\
**Category:** Teaching & Outreach\
**Created:** [November 15, 2021, 12:54am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471 "2021-11-15T00:54:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [November 15, 2021, 12:54am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/1 "2021-11-15T00:54:51Z")

</div>

In Julia 1.3, this used to work a charm:

```julia
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).

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [November 15, 2021, 1:54am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/2 "2021-11-15T01:54:53Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [November 15, 2021, 2:14am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/3 "2021-11-15T02:14:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 15, 2021, 2:25am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/4 "2021-11-15T02:25:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ianshmean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ianshmean/32/216042_2.png) [@ianshmean](https://discourse.julialang.org/u/ianshmean)\
**Post date:** [November 15, 2021, 6:27am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/5 "2021-11-15T06:27:51Z")

</div>

Or the easier to write

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

```

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [November 15, 2021, 8:37am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/6 "2021-11-15T08:37:48Z")

</div>

Perfect, thanks

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 15, 2021, 10:22am UTC](https://discourse.julialang.org/t/looking-for-simple-example-to-explain-ahead-of-time-compilation/71471/7 "2021-11-15T10:22:16Z")

</div>

Just use vector inputs.

```julia
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.
