Skip to content
Snippets Groups Projects
Commit c53da705 authored by Miroslav Kratochvil's avatar Miroslav Kratochvil :bicyclist:
Browse files

finish/clean up julia2 course slides

parent 89233fe6
No related branches found
No related tags found
2 merge requests!174Mk julia5 hw3,!164finish/clean up julia2 course slides
......@@ -6,82 +6,11 @@
<i class="twa twa-mechanical-leg"></i>
<br>
Compiler &amp; language internals
(part 1)
</div>
# What is this "compiled code"?
You can inspect various stages of code representation (and reveal bugs):
- `code_lowered(+)`
- `code_typed(+)`
- `code_typed(+, (Int, Float64))`
- `code_llvm`
- `code_native`
What happens if we use an abstract type?
<center style="font-size:200%; line-height:120%; margin-bottom: 1em">
Performance rule #1:<br><br>
<strong>Less instructions is typically better,</strong><br>
<strong>less jumps is typically much better.</strong>
</center>
<center style="font-size:200%; line-height:120%; margin-bottom: 1em">
Performance rule #2:<br><br>
<strong>Memory allocations comprise of many instructions<br> and plenty of jumps.</strong>
</center>
# What are all these things starting with `@`?
Macros allow you to generate code before compiler runs it:
- `@info` can know what variable names to display
- `@benchmark` can run the code several times
- `@test` can safely wrap the failing code and print what failed
## Important types for macros:
- symbols: `:abc`, `:+`, ...
- expressions: `:(1+2)`, `quote ... end`
Macros are functions that get unevaluated arguments (as expressions) and return unevaluated results (as expressions).
```julia
macro clear_data(v)
quote
$(Symbol(v, :_orig)) = load_original_data()
$(Symbol(v, :_enriched)) = nothing
$(Symbol(v, :_final)) = nothing
end
end
@clear_data patients # reloads patients_orig, clears patients_enriched, patients_final
```
# Macro use-cases
Use macros to improve your code whenever Julia syntax is too unwieldy.
```julia
model = @reaction_network MyModel begin
c1, s + i --> 2i
c2, i --> r
end
```
(Taken from `Catalyst.jl`)
# How do I package my code?
Use `] generate MyPackageName`.
......@@ -106,24 +35,3 @@ using Test, MyPackageName
@test f() == 123
end
```
# How do I document my code?
Docstrings are automatically sourced and exposed to the help system (and `?`)
```julia
"""
f()
This returns a pretty good randomly chosen constant.
"""
f() = 123
```
Extra packages:
- `Documenter.jl` makes a nice website out of the documentation
- (and uploads it to e.g. github pages)
- `DocStringExtensions.jl` provide some macros to simplify writing docstrings
- (e.g., typesetting of the full function type signature)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment