Skip to content

Commit 08d107b

Browse files
committed
add benchmarks to test matrix changes
1 parent 2266634 commit 08d107b

File tree

3 files changed

+419
-0
lines changed

3 files changed

+419
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Two hacks
2+
3+
4+
#InstallMethod( \[\], [ IsMatrix, IsList ], {m,l} -> m[l[1]][l[2]] );
5+
InstallOtherMethod( \[\], [ IsMatrix, IsPosInt, IsPosInt ], {m,i,j} -> m[i][j] );
6+
InstallOtherMethod( MatElm, [IsMatrix,IsPosInt,IsPosInt], {m,i,j} -> m[i][j] );
7+
8+
9+
m:=List([1..10],i->List([1..10],j-> (i-1)*10 + j));
10+
11+
12+
gap> m:=IdentityMat(10);;
13+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i][j]; od; od; od; time;
14+
62
15+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i,j]; od; od; od; time;
16+
596
17+
gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;
18+
19+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MatElm(m,i,j); od; od; od; time;
20+
525
21+
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
22+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=f(m,i,j); od; od; od; time;
23+
54
24+
25+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MATELM_PLIST(m,i,j); od; od; od; time;
26+
54
27+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MATELM_LIST(m,i,j); od; od; od; time;
28+
63
29+
30+
31+
gap> m:=IdentityMat(10,GF(2));;
32+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i][j]; od; od; od; time;
33+
68
34+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i,j]; od; od; od; time;
35+
468
36+
gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;
37+
38+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MatElm(m,i,j); od; od; od; time;
39+
379
40+
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
41+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=f(m,i,j); od; od; od; time;
42+
135
43+
44+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MATELM_LIST(m,i,j); od; od; od; time;
45+
81
46+
47+
48+
gap> m:=IdentityMat(10,GF(2));; ConvertToMatrixRep(m);;
49+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i][j]; od; od; od; time;
50+
152 # note: SLOWER
51+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=m[i,j]; od; od; od; time;
52+
354
53+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MatElm(m,i,j); od; od; od; time;
54+
227
55+
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
56+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=f(m,i,j); od; od; od; time;
57+
224
58+
59+
60+
gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;
61+
gap> for i in [1..10] do for j in [1..10] do Assert(0, MAT_ELM_GF2MAT(m,i,j) = m[i][j]); od; od;
62+
63+
64+
InstallOtherMethod( \[\], [ IsGF2MatrixRep, IsPosInt, IsPosInt ], MAT_ELM_GF2MAT );
65+
InstallMethod( MatElm, [ IsGF2MatrixRep, IsPosInt, IsPosInt ], MAT_ELM_GF2MAT );
66+
67+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MAT_ELM_GF2MAT(m,i,j); od; od; od; time;
68+
60
69+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MatElm(m,i,j); od; od; od; time;
70+
65
71+
72+
gap> for u in [1..10000] do for i in [1..10] do for j in [1..10] do x:=MATELM_LIST(m,i,j); od; od; od; time;

benchmark/matobj/bench-mat.g

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
ReadGapRoot("bench.g");
2+
3+
# TODO: the following helper functions are meant to allow writing
4+
# code that uses (Set)MatElm but still accepts "old-style" matrices.
5+
# as input. We might want to add them to the library... or maybe not,
6+
# as new code probably should use mat[i,j] directly instaed.
7+
InstallOtherMethod( MatElm, [ IsMatrix, IsPosInt, IsPosInt ],
8+
-RankFilter(IsMatrix),
9+
{m,i,j} -> m[i,j] );
10+
InstallOtherMethod( SetMatElm, [ IsMatrix, IsPosInt, IsPosInt, IsObject ],
11+
-RankFilter(IsMatrix),
12+
function(m,i,j,v) m[i,j]:=v; end);
13+
14+
# TODO: add tests for all of this
15+
# TODO: also test Unbind(mat[i,j]) / IsBound(mat[i,j]) ?!? then again, we might not want
16+
# to support these at all, given that matrix objects should be dense
17+
# TODO: also add tests for out-of-bounds array indices
18+
19+
20+
PrintBoxed := function(str)
21+
local n, line;
22+
n := Length(str) + 2;
23+
line := Concatenation("+", ListWithIdenticalEntries(n,'-'), "+\n");
24+
Print(line);
25+
Print("| ", str, " |\n");
26+
Print(line);
27+
end;
28+
29+
PrintHeadline := function(what);
30+
Print(TextAttr.5, "Testing ", what, ":\n",TextAttr.reset);
31+
end;
32+
33+
MyBench := function(func)
34+
local opt, res;
35+
36+
opt := rec(
37+
mintime:=300,
38+
showSummary := false
39+
);
40+
res := Benchmark(func, opt);
41+
42+
Print(" ", Round(res.counter * 1000.0 / res.total ), " iterations per second\n");
43+
44+
# Print(" Performed ", res.counter, " iterations, taking ", Round(res.total), " milliseconds; ");
45+
# #Print("timings: ", timings, "\n");
46+
# # Print("average: ", Round(100*res.avg)/100,
47+
# # " +/- ", Round(100*res.std)/100,
48+
# # " (+/- ", Round(100*res.std/res.avg), "%)",
49+
# # "\n");
50+
# Print(" median: ", res.median, "\n");
51+
end;
52+
53+
54+
TestReadingMatrix := function(m)
55+
local f;
56+
57+
PrintHeadline("m[i][j]");
58+
MyBench(function()
59+
local u, i, j, rows, cols, x;
60+
rows := [1..Length(m)];
61+
cols := [1..Length(m[1])];
62+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
63+
for i in rows do
64+
for j in cols do
65+
x:=m[i][j];
66+
od;
67+
od;
68+
od;
69+
end);
70+
71+
PrintHeadline("m[i,j]");
72+
MyBench(function()
73+
local u, i, j, rows, cols, x;
74+
rows := [1..Length(m)];
75+
cols := [1..Length(m[1])];
76+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
77+
for i in rows do
78+
for j in cols do
79+
x:=m[i,j];
80+
od;
81+
od;
82+
od;
83+
end);
84+
85+
PrintHeadline("MatElm(m,i,j)");
86+
MyBench(function()
87+
local u, i, j, rows, cols, x;
88+
rows := [1..Length(m)];
89+
cols := [1..Length(m[1])];
90+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
91+
for i in rows do
92+
for j in cols do
93+
x:=MatElm(m,i,j);
94+
od;
95+
od;
96+
od;
97+
end);
98+
99+
PrintHeadline("MatElm(m,i,j) with prefetched method");
100+
f:=ApplicableMethod(MatElm, [m,1,1]);;
101+
MyBench(function()
102+
local u, i, j, rows, cols, x;
103+
rows := [1..Length(m)];
104+
cols := [1..Length(m[1])];
105+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
106+
for i in rows do
107+
for j in cols do
108+
x:=f(m,i,j);
109+
od;
110+
od;
111+
od;
112+
end);
113+
114+
end;
115+
116+
TestWritingMatrix := function(m)
117+
local f;
118+
119+
PrintHeadline("m[i][j]:=elm");
120+
MyBench(function()
121+
local u, i, j, rows, cols, x;
122+
x:=m[1][1];
123+
rows := [1..Length(m)];
124+
cols := [1..Length(m[1])];
125+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
126+
for i in rows do
127+
for j in cols do
128+
m[i][j]:=x;
129+
od;
130+
od;
131+
od;
132+
end);
133+
134+
PrintHeadline("m[i,j]:=elm");
135+
MyBench(function()
136+
local u, i, j, rows, cols, x;
137+
x:=m[1][1];
138+
rows := [1..Length(m)];
139+
cols := [1..Length(m[1])];
140+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
141+
for i in rows do
142+
for j in cols do
143+
m[i,j]:=x;
144+
od;
145+
od;
146+
od;
147+
end);
148+
149+
PrintHeadline("SetMatElm(m,i,j,elm)");
150+
MyBench(function()
151+
local u, i, j, rows, cols, x;
152+
x:=m[1][1];
153+
rows := [1..Length(m)];
154+
cols := [1..Length(m[1])];
155+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
156+
for i in rows do
157+
for j in cols do
158+
SetMatElm(m,i,j,x);
159+
od;
160+
od;
161+
od;
162+
end);
163+
164+
PrintHeadline("SetMatElm(m,i,j,elm) with prefetched method");
165+
f:=ApplicableMethod(SetMatElm, [m,1,1,m[1][1]]);;
166+
MyBench(function()
167+
local u, i, j, rows, cols, x;
168+
x:=m[1][1];
169+
rows := [1..Length(m)];
170+
cols := [1..Length(m[1])];
171+
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
172+
for i in rows do
173+
for j in cols do
174+
f(m,i,j,x);
175+
od;
176+
od;
177+
od;
178+
end);
179+
end;
180+
181+
RunMatTest := function(desc, m)
182+
Print("\n");
183+
PrintBoxed(Concatenation("Testing ", desc));
184+
TestReadingMatrix(m);
185+
Print(TextAttr.2, "...now testing write access...\n", TextAttr.reset);
186+
TestWritingMatrix(m);
187+
end;
188+
189+
m:=IdentityMat(10);;
190+
RunMatTest("integer matrix", m);
191+
192+
m:=IdentityMat(10,GF(2));;
193+
RunMatTest("GF(2) rowlist", m);
194+
195+
m:=IdentityMat(10,GF(2));; ConvertToMatrixRep(m);;
196+
RunMatTest("GF(2) compressed matrix", m);
197+
198+
m:=IdentityMat(10,GF(7));;
199+
RunMatTest("GF(7) rowlist", m);
200+
201+
m:=IdentityMat(10,GF(7));; ConvertToMatrixRep(m);;
202+
RunMatTest("GF(7) compressed matrix", m);

0 commit comments

Comments
 (0)