Skip to content

Commit 8a8366a

Browse files
committed
Improve mwe_03.jl
1 parent 5126a8b commit 8a8366a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

mwes/mwe_03.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# fix allocations of this array comprehension:
1+
# Fix allocations of this array comprehension:
22
# y = [panel.control_point[2] for panel in body_aero.panels]
3+
# This mwe shows that the line above is allocating a lot,
4+
# and provides the function test_new() that shows a better way of implementing.
35

46
using VortexStepMethod, PreallocationTools
57

@@ -31,17 +33,30 @@ body_aero::BodyAerodynamics = BodyAerodynamics([wing])
3133
y = [panel.control_point[2] for panel in body_aero.panels]
3234
n = @allocated y = [panel.control_point[2] for panel in body_aero.panels]
3335

34-
function test(body_aero, gamma_i)
36+
function test_old(body_aero, gamma_i)
37+
y = [panel.control_point[2] for panel in body_aero.panels]
38+
y
39+
end
40+
41+
function test_new(body_aero, gamma_i)
3542
y = body_aero.y
3643
for (i, panel) in pairs(body_aero.panels)
3744
y[i] = panel.control_point[2]
3845
end
39-
# y = [panel.control_point[2] for panel in body_aero.panels]
4046
y
4147
end
4248

49+
4350
gamma_i=zeros(length(body_aero.panels))
4451

45-
test(body_aero, gamma_i)
46-
@allocated test(body_aero, gamma_i)
52+
# make sure the two test functions get compiled
53+
test_old(body_aero, gamma_i)
54+
test_new(body_aero, gamma_i)
55+
56+
println("Old function, using an array comprehension:")
57+
n = @allocated test_old(body_aero, gamma_i)
58+
println("Allocations: ", n)
4759

60+
println("New function, using a pre-allocated array and a for loop:")
61+
m = @allocated test_new(body_aero, gamma_i)
62+
println("Allocations: ", m)

0 commit comments

Comments
 (0)