-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharray-loops-advanced.jl
More file actions
131 lines (122 loc) · 3.58 KB
/
array-loops-advanced.jl
File metadata and controls
131 lines (122 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using BenchmarkTools
a = zeros(100,100)
# map and broadcast use the @inbounds macro to avoid bounds checking
# hence the @inbounds was added to the explicit loops in order to have a fair comparison
## The results are quite different without the @inbounds macro
## Assignment functions
function row_col_loop(a::AbstractArray)
output = similar(a)
nrows, ncols = size(a)
@inbounds for row in 1:nrows
for col in 1:ncols
output[row, col] = a[row, col]^2
end
end
end;
function col_row_loop(a::AbstractArray)
output = similar(a)
nrows, ncols = size(a)
@inbounds for col in 1:ncols
for row in 1:nrows
output[row, col] = a[row, col]^2
end
end
end;
function length_loop(a::AbstractArray)
output = similar(a)
@inbounds for i in 1:length(a)
output[i] = a[i]^2
end
end;
function eachindex_loop(a::AbstractArray)
output = similar(a)
@inbounds for id in eachindex(a)
output[id] = a[id]^2
end
end;
function broadcast_loop(a::AbstractArray)
output = broadcast(x->x^2, a)
end;
function dot_broadcast_loop(a::AbstractArray)
output = a .^ 2
end;
function map_loop(a::AbstractArray)
output = map(x->x^2, a)
end;
function comprehension_loop(a::AbstractArray)
output = [x^2 for x in a]
end;
## Mutation functions
function row_col_loop!(a::AbstractArray)
nrows, ncols = size(a)
@inbounds for row in 1:nrows
for col in 1:ncols
a[row, col] = a[row, col]^2
end
end
end;
function col_row_loop!(a::AbstractArray)
nrows, ncols = size(a)
@inbounds for col in 1:ncols
for row in 1:nrows
a[row, col] = a[row, col]^2
end
end
end;
function eachindex_loop!(a::AbstractArray)
@inbounds for id in eachindex(a)
a[id] = a[id]^2
end
end;
function length_loop!(a::AbstractArray)
@inbounds for i in 1:length(a)
a[i] = a[i]^2
end
end;
function broadcast_loop!(a::AbstractArray)
broadcast!(x->x^2, a, a)
end;
function dot_broadcast_loop!(a::AbstractArray)
a .= a .^ 2
end;
function map_loop!(a::AbstractArray)
map!(x->x^2, a, a)
end;
function comprehension_loop!(a::AbstractArray)
a .= [x^2 for x in a]
end;
println()
println("-----------BENCHMARK-----------")
println()
println("Compute element-wise square of a 100x100 matrix in a new matrix")
print("row -> col: "); @btime row_col_loop(a);
print("col -> row: "); @btime col_row_loop(a);
print("eachindex: "); @btime eachindex_loop(a);
print("length: "); @btime length_loop(a);
print("broadcast: "); @btime broadcast_loop(a);
print("dot broadcast: "); @btime dot_broadcast_loop(a);
print("map: "); @btime map_loop(a);
print("comprehension: "); @btime comprehension_loop(a);
print("row -> col!: "); @btime row_col_loop!(a);
print("col -> row!: "); @btime col_row_loop!(a);
print("eachindex!: "); @btime eachindex_loop!(a);
print("length!: "); @btime length_loop!(a);
print("broadcast!: "); @btime broadcast_loop!(a);
print("dot broadcast!:"); @btime dot_broadcast_loop!(a);
print("map!: "); @btime map_loop!(a);
print("comprehension!:"); @btime comprehension_loop!(a);
function sum_loop(a::AbstractArray)
output = zero(eltype(a))
@inbounds for x in a
output += x
end
return output
end;
println()
println("-----------BENCHMARK-----------")
println()
println("Compute the sum of a 100x100 matrix")
print("sum (alias of reduce(+,x)):");@btime sum(a); # in fact + is replace by add_sum
print("reduce: ");@btime reduce(+, a);
print("loop: ");@btime sum_loop(a);
print()