You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lecture_05/lecture.md
+46-49Lines changed: 46 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -857,73 +857,70 @@ So when you use closures, you should be careful of the accidental boxing, since
857
857
It happens a lot in scientific code, that some experiments has many parameters. It is therefore very convenient to store them in `Dict`, such that when adding a new parameter, we do not have to go over all defined functions and redefine them.
858
858
859
859
Imagine that we have a (nonsensical) simulation like
from the profiler's output [here](profile6.svg) we can see some type instabilities. Where they come from?
881
+
The compiler does not have any infomation about types stored in `settings`, as the type of stored values are `Any` (caused by storing `String` and `Int`).
882
+
```julia
883
+
julia>typeof(settings)
884
+
Dict{Symbol, Any}
885
885
```
886
-
887
-
- create a list of examples for the lecture
888
-
889
-
## Using named tuple instead of dict
886
+
The second problem is `get` operation on dictionaries is very time consuming operation (although technically it is O(1)), because it has to search the key in the list. Dicts are designed as a mutable container, which is not needed in our use-case, as the settings are static. For similar use-cases, Julia offers `NamedTuple`, with which we can construct settings as
890
887
```julia
891
-
892
-
## Performance of captured variable
893
-
- inspired by https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured
894
-
- an example of closure seen inprevious lectures, reference `x` has to be included
The `NamedTuple` is fully typed, but which we mean the names of fields are part of the type definition and fields are also part of type definition. You can think of it as a struct. Moreover, when accessing fields in `NamedTuple`, compiler knows precisely where they are located in the memory, which drastically reduces the access time.
891
+
Let's see the effect in `BenchmarkTools`.
895
892
```julia
896
-
x = rand(1000)
893
+
julia>@benchmarkfind_min!(x -> x^2, x₀, settings)
894
+
BenchmarkTools.Trial:10000 samples with 1 evaluation.
895
+
Range (min … max):86.350 μs …4.814 ms ┊ GC (min … max):0.00%…97.61%
0 commit comments