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/lab.md
-177Lines changed: 0 additions & 177 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,46 +56,6 @@ We are getting a little ahead of ourselves in this lab, as understanding of thes
56
56
- run the function with the argument once before running `@time` or use `@btime` if you have `BenchmarkTools` readily available in your environment
57
57
- To see some measurable difference with this simple function, a longer vector of coefficients may be needed.
58
58
59
-
!!! details
60
-
```@repl lab05_polynomial
61
-
function polynomial_stable(a, x)
62
-
accumulator = zero(x)
63
-
for i in length(a):-1:1
64
-
accumulator += x^(i-1) * a[i]
65
-
end
66
-
accumulator
67
-
end
68
-
```
69
-
70
-
```@repl lab05_polynomial
71
-
@code_warntype polynomial_stable(a, x) # type stable
72
-
@code_warntype polynomial_stable(a, xf) # type stable
73
-
```
74
-
75
-
```@repl lab05_polynomial
76
-
polynomial(a, xf) #hide
77
-
polynomial_stable(a, xf) #hide
78
-
@time polynomial(a, xf)
79
-
@time polynomial_stable(a, xf)
80
-
```
81
-
82
-
Only really visible when evaluating multiple times.
83
-
```julia
84
-
julia> using BenchmarkTools
85
-
86
-
julia> @btime polynomial($a, $xf)
87
-
31.806 ns (0 allocations: 0 bytes)
88
-
128.0
89
-
90
-
julia> @btime polynomial_stable($a, $xf)
91
-
28.522 ns (0 allocations: 0 bytes)
92
-
128.0
93
-
```
94
-
Difference only a few nanoseconds.
95
-
96
-
97
-
*Note*: Recalling homework from lab 1. Adding `zero` also extends this function to the case of `x` being a matrix, see `?` menu.
98
-
99
59
100
60
Code stability issues are something unique to Julia, as its JIT compilation allows it to produce code that contains boxed variables, whose type can be inferred during runtime. This is one of the reasons why interpreted languages are slow to run but fast to type. Julia's way of solving it is based around compiling functions for specific arguments, however in order for this to work without the interpreter, the compiler has to be able to infer the types.
101
61
@@ -201,22 +161,6 @@ In order to get more of a visual feel for profiling, there are packages that all
201
161
Let's compare this with the type unstable situation.
202
162
203
163
204
-
!!! details
205
-
First let's define the function that allows us to run the `polynomial` multiple times.
206
-
```@repl lab05_polynomial
207
-
function run_polynomial(a, x, n)
208
-
for _ in 1:n
209
-
polynomial(a, x)
210
-
end
211
-
end
212
-
```
213
-
214
-
```julia
215
-
@profview run_polynomial(a, xf, Int(1e5)) # clears the profile for us
216
-
```
217
-

218
-
219
-
220
164
Other options for viewing profiler outputs
221
165
-[ProfileView](https://github.com/timholy/ProfileView.jl) - close cousin of `ProfileSVG`, spawns GTK window with interactive FlameGraph
222
166
-[VSCode](https://www.julia-vscode.org/docs/stable/release-notes/v0_17/#Profile-viewing-support-1) - always imported `@profview` macro, flamegraphs (js extension required), filtering, one click access to source code
@@ -235,47 +179,6 @@ We have noticed that no matter if the function is type stable or unstable the ma
235
179
[^1]: Explanation of the Horner schema can be found on [https://en.wikipedia.org/wiki/Horner%27s\_method](https://en.wikipedia.org/wiki/Horner%27s_method).
236
180
237
181
238
-
!!! details
239
-
```julia
240
-
function polynomial(a, x)
241
-
accumulator = a[end] * one(x)
242
-
for i in length(a)-1:-1:1
243
-
accumulator = accumulator * x + a[i]
244
-
end
245
-
accumulator
246
-
end
247
-
```
248
-
249
-
Speed up:
250
-
- 49ns -> 8ns ~ 6x on integer valued input
251
-
- 59ns -> 8ns ~ 7x on real valued input
252
-
253
-
```
254
-
julia> @btime polynomial($a, $x)
255
-
8.008 ns (0 allocations: 0 bytes)
256
-
97818
257
-
258
-
julia> @btime polynomial_stable($a, $x)
259
-
49.173 ns (0 allocations: 0 bytes)
260
-
97818
261
-
262
-
julia> @btime polynomial($a, $xf)
263
-
8.008 ns (0 allocations: 0 bytes)
264
-
97818.0
265
-
266
-
julia> @btime polynomial_stable($a, $xf)
267
-
58.773 ns (0 allocations: 0 bytes)
268
-
97818.0
269
-
```
270
-
These numbers will be different on different HW.
271
-
272
-
**BONUS**: The profile trace does not even contain the calling of mathematical operators and is mainly dominated by the iteration utilities. In this case we had to increase the number of runs to `1e6` to get some meaningful trace.
0 commit comments