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
+98-97Lines changed: 98 additions & 97 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -795,6 +795,104 @@ BenchmarkTools.Trial: 2440 samples with 1 evaluation.
795
795
```
796
796
By not checking the bounds, we bring the speed close to the version based on matrix multiplication, while having small memory requirements (further speedup can be achieved using threadding).
797
797
798
+
## NamedTuples are more efficient that Dicts
799
+
It happens a lot in scientific code, that some experiments have 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.
800
+
801
+
Imagine that we have a (nonsensical) simulation like
from the profiler's output [here](profiles/profile6.html) we can see some type instabilities. Where they come from?
822
+
The compiler does not have any information about types stored in `settings`, as the type of stored values are `Any` (caused by storing `String` and `Int`).
823
+
```julia
824
+
julia>typeof(settings)
825
+
Dict{Symbol, Any}
826
+
```
827
+
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. `Dict`s 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
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.
832
+
Let's see the effect in `BenchmarkTools`.
833
+
```julia
834
+
julia>@benchmarkfind_min!(x -> x^2, x₀, settings)
835
+
BenchmarkTools.Trial:10000 samples with 1 evaluation.
836
+
Range (min … max):86.350 μs …4.814 ms ┊ GC (min … max):0.00%…97.61%
Recall closure is a function which contains some parameters contained
800
898
@@ -954,100 +1052,3 @@ No errors !
954
1052
955
1053
So when you use closures, you should be careful of the accidental boxing, since it can inhibit the speed of code. **This is a big deal in Multithreadding and in automatic differentiation**, both heavily uses closures. You can track the discussion [here](https://github.com/JuliaLang/julia/issues/15276).
956
1054
957
-
958
-
## NamedTuples are more efficient that Dicts
959
-
It happens a lot in scientific code, that some experiments have 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.
960
-
961
-
Imagine that we have a (nonsensical) simulation like
from the profiler's output [here](profiles/profile6.html) we can see some type instabilities. Where they come from?
982
-
The compiler does not have any information about types stored in `settings`, as the type of stored values are `Any` (caused by storing `String` and `Int`).
983
-
```julia
984
-
julia>typeof(settings)
985
-
Dict{Symbol, Any}
986
-
```
987
-
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. `Dict`s 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
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.
992
-
Let's see the effect in `BenchmarkTools`.
993
-
```julia
994
-
julia>@benchmarkfind_min!(x -> x^2, x₀, settings)
995
-
BenchmarkTools.Trial:10000 samples with 1 evaluation.
996
-
Range (min … max):86.350 μs …4.814 ms ┊ GC (min … max):0.00%…97.61%
0 commit comments