-
Notifications
You must be signed in to change notification settings - Fork 147
/
README_benchmarks.jl
62 lines (58 loc) · 2.36 KB
/
README_benchmarks.jl
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
using BenchmarkTools
using LinearAlgebra
using StaticArrays
add!(C, A, B) = (C .= A .+ B)
function simple_bench(N, T=Float64)
A = rand(T,N,N)
A = A'*A
B = copy(A)
SA = SMatrix{N,N}(A)
MA = MMatrix{N,N}(A)
MB = copy(MA)
print("""
============================================
Benchmarks for $N×$N $T matrices
============================================
""")
ops = [
("Matrix multiplication ", *, (A, A), (SA, SA)),
("Matrix multiplication (mutating) ", mul!, (B, A, A), (MB, MA, MA)),
("Matrix addition ", +, (A, A), (SA, SA)),
("Matrix addition (mutating) ", add!, (B, A, A), (MB, MA, MA)),
("Matrix determinant ", det, (A,), (SA,)),
("Matrix inverse ", inv, (A,), (SA,)),
("Matrix symmetric eigendecomposition", eigen, (A,), (SA,)),
("Matrix Cholesky decomposition ", cholesky, (A,), (SA,)),
("Matrix LU decomposition ", lu, (A,), (SA,)),
("Matrix QR decomposition ", qr, (A,), (SA,)),
]
for (name, op, Aargs, SAargs) in ops
# We load from Ref's here to avoid the compiler completely removing the
# benchmark in some cases.
#
# Like any microbenchmark, the speedups you see here should only be
# taken as roughly indicative of the speedup you may see in real code.
if length(Aargs) == 1
A1 = Ref(Aargs[1])
SA1 = Ref(SAargs[1])
speedup = @belapsed($op($A1[])) / @belapsed($op($SA1[]))
elseif length(Aargs) == 2
A1 = Ref(Aargs[1])
A2 = Ref(Aargs[2])
SA1 = Ref(SAargs[1])
SA2 = Ref(SAargs[2])
speedup = @belapsed($op($A1[], $A2[])) / @belapsed($op($SA1[], $SA2[]))
elseif length(Aargs) == 3
A1 = Ref(Aargs[1])
A2 = Ref(Aargs[2])
A3 = Ref(Aargs[3])
SA1 = Ref(SAargs[1])
SA2 = Ref(SAargs[2])
SA3 = Ref(SAargs[3])
speedup = @belapsed($op($A1[], $A2[], $A3[])) / @belapsed($op($SA1[], $SA2[], $SA3[]))
else
end
println(name*" -> $(round(speedup, digits=1))x speedup")
end
end
simple_bench(3)