Skip to content

[jax_intro] Update %time magic with %timeit #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lectures/jax_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.16.1
jupytext_version: 1.16.6
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -348,7 +348,15 @@ The compiled versions for the previous array size are still available in memory
too, and the following call is dispatched to the correct compiled code.

```{code-cell} ipython3
%time f(x).block_until_ready()
%timeit f(x).block_until_ready()
```

```{note}
Note that we use the [`%timeit` magic](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit)
here to get more reliable timing statistics.

It runs the code multiple times to calculate both mean and standard deviation,
which takes longer but provides more accurate measurements than a single run.
```

### Compiling the outer function
Expand All @@ -368,14 +376,13 @@ f_jit(x)
And now let's time it.

```{code-cell} ipython3
%time f_jit(x).block_until_ready()
%timeit f_jit(x).block_until_ready()
```

Note the speed gain.

This is because the array operations are fused and no intermediate arrays are created.


Incidentally, a more common syntax when targetting a function for the JIT
compiler is

Expand Down