Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
courses
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
R3
school
courses
Commits
c53da705
Commit
c53da705
authored
2 years ago
by
Miroslav Kratochvil
Browse files
Options
Downloads
Patches
Plain Diff
finish/clean up julia2 course slides
parent
89233fe6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!174
Mk julia5 hw3
,
!164
finish/clean up julia2 course slides
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md
+1
-93
1 addition, 93 deletions
2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md
with
1 addition
and
93 deletions
2023/2023-03-09_ProgrammingWithJulia-2/slides/2a-details.md
+
1
−
93
View file @
c53da705
...
...
@@ -6,82 +6,11 @@
<i
class=
"twa twa-mechanical-leg"
></i>
<br>
Compiler
&
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
-->
2
i
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)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment