forked from MichielStock/Kronecker.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks.jl
125 lines (96 loc) · 2.51 KB
/
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
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
#=
Created on Tuesday 2 July 2019
Last update: Saturday 27 July 2019
@author: Michiel Stock
Benchmarking Kroncker.jl compated to native functions.
=#
using Kronecker, Plots, LinearAlgebra
using BenchmarkTools
sizes = [5, 10, 25, 50, 100, 250, 500, 1000, 5000]
tmax = 10
# inverse
times_kron = []
times_naive = []
for s in sizes
A = randn(s, s) / s^2
B = rand(s, s) / s^2
v = rand(s^2)
K = A ⊗ B
t = @belapsed inv($K)
push!(times_kron, t)
if s^2 < 5000
K = collect(K)
t = @belapsed inv($K)
push!(times_naive, t)
compute_naive = t < tmax
end
end
plot(sizes.^2, times_kron, label="matrix inverse", color=:blue, legend=:bottomright, lw=2)
plot!(sizes[1:length(times_naive)].^2, times_naive, color=:blue, ls=:dash, label="", lw=2 )
# determinant
times_kron = []
times_naive = []
@belapsed det(Kwarmup)
for s in sizes
A = randn(s, s)
B = rand(s, s)
v = rand(s^2)
K = A ⊗ B
t = @belapsed det($K)
push!(times_kron, t)
if s^2 < 5000
K = collect(K)
t = @belapsed det($K)
push!(times_naive, t)
compute_naive = t < tmax
end
end
plot!(sizes.^2, times_kron, label="determinant", color=:green, lw=2)
plot!(sizes[1:length(times_naive)].^2, times_naive, color=:green, ls=:dash, label="", lw=2)
# squaring
times_kron = []
times_naive = []
v = rand(100)
@belapsed Kwarmup * Kwarmup
for s in sizes
A = randn(s, s)
B = rand(s, s)
v = rand(s^2)
K = A ⊗ B
t = @belapsed $K * $K
push!(times_kron, t)
if s^2 < 5000
K = collect(K)
t = @belapsed $K * $K
push!(times_naive, t)
compute_naive = t < tmax
end
end
plot!(sizes.^2, times_kron, label="squaring", color=:red, lw=2)
plot!(sizes[1:length(times_naive)].^2, times_naive, color=:red, ls=:dash, label="", lw=2)
# vector multiplication
times_kron = []
times_naive = []
for s in sizes
A = randn(s, s)
B = rand(s, s)
v = rand(s^2)
K = A ⊗ B
t = @belapsed $K * $v
push!(times_kron, t)
if s^2 < 5000
K = collect(K)
t = @belapsed $K * $v
push!(times_naive, t)
compute_naive = t < tmax
end
end
plot!(sizes.^2, times_kron, label="matrix-vector mult.", color=:orange, lw=2)
plot!(sizes[1:length(times_naive)].^2, times_naive, color=:orange, ls=:dash, label="", lw=2)
yaxis!(:log10)
ylabel!("CPU time (s)")
xaxis!(:log10)
xlabel!("Kronecker product size")
title!("Performance Kronecker.jl (-) vs. native code (--)")
savefig("benchmark.svg")