Skip to content

Commit 23699e1

Browse files
authored
Add benchmarks for ExtractSubMatrix + CopySubMatrix (#5958)
These functions are part of the MatrixObj AP, but currently they are basically always slower than their list arithmetic counter parts involving the `m{}{}` syntax, even in situations where those APIs were supposed to be superior. I believe this can be reversed by providing tuned methods for these functions (at least for the built-in plain and compressed matrices). In order to tackler this systematically, we need robust benchmarks to measure the effect of any changes.
1 parent b47f8a4 commit 23699e1

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

benchmark/matobj/bench-matelm-access.g

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ TestReadingMatrix := function(m)
77
PrintHeadline("m[i][j]");
88
MyBench(function()
99
local u, i, j, rows, cols, x;
10-
rows := [1..Length(m)];
11-
cols := [1..Length(m[1])];
12-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
10+
rows := [1..NrRows(m)];
11+
cols := [1..NrCols(m)];
12+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
1313
for i in rows do
1414
for j in cols do
1515
x:=m[i][j];
@@ -21,9 +21,9 @@ TestReadingMatrix := function(m)
2121
PrintHeadline("m[i,j]");
2222
MyBench(function()
2323
local u, i, j, rows, cols, x;
24-
rows := [1..Length(m)];
25-
cols := [1..Length(m[1])];
26-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
24+
rows := [1..NrRows(m)];
25+
cols := [1..NrCols(m)];
26+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
2727
for i in rows do
2828
for j in cols do
2929
x:=m[i,j];
@@ -35,9 +35,9 @@ TestReadingMatrix := function(m)
3535
PrintHeadline("MatElm(m,i,j)");
3636
MyBench(function()
3737
local u, i, j, rows, cols, x;
38-
rows := [1..Length(m)];
39-
cols := [1..Length(m[1])];
40-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
38+
rows := [1..NrRows(m)];
39+
cols := [1..NrCols(m)];
40+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
4141
for i in rows do
4242
for j in cols do
4343
x:=MatElm(m,i,j);
@@ -50,9 +50,9 @@ TestReadingMatrix := function(m)
5050
f:=ApplicableMethod(MatElm, [m,1,1]);;
5151
MyBench(function()
5252
local u, i, j, rows, cols, x;
53-
rows := [1..Length(m)];
54-
cols := [1..Length(m[1])];
55-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
53+
rows := [1..NrRows(m)];
54+
cols := [1..NrCols(m)];
55+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
5656
for i in rows do
5757
for j in cols do
5858
x:=f(m,i,j);
@@ -70,9 +70,9 @@ TestWritingMatrix := function(m)
7070
MyBench(function()
7171
local u, i, j, rows, cols, x;
7272
x:=m[1][1];
73-
rows := [1..Length(m)];
74-
cols := [1..Length(m[1])];
75-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
73+
rows := [1..NrRows(m)];
74+
cols := [1..NrCols(m)];
75+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
7676
for i in rows do
7777
for j in cols do
7878
m[i][j]:=x;
@@ -85,9 +85,9 @@ TestWritingMatrix := function(m)
8585
MyBench(function()
8686
local u, i, j, rows, cols, x;
8787
x:=m[1][1];
88-
rows := [1..Length(m)];
89-
cols := [1..Length(m[1])];
90-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
88+
rows := [1..NrRows(m)];
89+
cols := [1..NrCols(m)];
90+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
9191
for i in rows do
9292
for j in cols do
9393
m[i,j]:=x;
@@ -100,9 +100,9 @@ TestWritingMatrix := function(m)
100100
MyBench(function()
101101
local u, i, j, rows, cols, x;
102102
x:=m[1][1];
103-
rows := [1..Length(m)];
104-
cols := [1..Length(m[1])];
105-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
103+
rows := [1..NrRows(m)];
104+
cols := [1..NrCols(m)];
105+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
106106
for i in rows do
107107
for j in cols do
108108
SetMatElm(m,i,j,x);
@@ -116,9 +116,9 @@ TestWritingMatrix := function(m)
116116
MyBench(function()
117117
local u, i, j, rows, cols, x;
118118
x:=m[1][1];
119-
rows := [1..Length(m)];
120-
cols := [1..Length(m[1])];
121-
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
119+
rows := [1..NrRows(m)];
120+
cols := [1..NrCols(m)];
121+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
122122
for i in rows do
123123
for j in cols do
124124
f(m,i,j,x);

benchmark/matobj/bench-submat.g

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
ReadGapRoot("benchmark/matobj/bench.g");
2+
3+
4+
TestExtractingSubmatrix := function(m)
5+
local rows, cols;
6+
7+
rows := [2..NrRows(m)-2];
8+
cols := [3..NrCols(m)-1];
9+
10+
PrintHeadline("m{}{}");
11+
MyBench(function()
12+
local u, x;
13+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
14+
x:=m{rows}{cols};
15+
od;
16+
end);
17+
18+
PrintHeadline("ExtractSubMatrix");
19+
MyBench(function()
20+
local u, x;
21+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
22+
x:=ExtractSubMatrix(m, rows, cols);
23+
od;
24+
end);
25+
26+
end;
27+
28+
TestCopyingSubmatrix := function(m)
29+
local x, rows, cols;
30+
31+
x := MutableCopyMat(m);
32+
rows := [2..NrRows(m)-2];
33+
cols := [3..NrCols(m)-1];
34+
35+
PrintHeadline("m{}{}:=n{}{}");
36+
MyBench(function()
37+
local u;
38+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
39+
x{rows}{cols}:=m{rows}{cols};
40+
od;
41+
end);
42+
43+
PrintHeadline("CopySubMatrix");
44+
MyBench(function()
45+
local u;
46+
for u in [1..QuoInt(100000,NrRows(m)*NrCols(m))] do
47+
CopySubMatrix(m, x, rows, cols, rows, cols);
48+
od;
49+
end);
50+
51+
end;
52+
53+
RunMatTest := function(desc, m)
54+
Print("\n");
55+
PrintBoxed(Concatenation("Testing submatrix extraction for ", desc));
56+
TestExtractingSubmatrix(m);
57+
Print(TextAttr.2, "...now testing submatrix copying...\n", TextAttr.reset);
58+
TestCopyingSubmatrix(m);
59+
end;
60+
61+
for dim in [10, 100] do
62+
PrintBoxed(Concatenation("Now testing in dimension ", String(dim)));
63+
64+
m:=IdentityMat(dim);;
65+
RunMatTest("integer matrix", m);
66+
67+
m:=IdentityMat(dim,GF(2));;
68+
RunMatTest("GF(2) rowlist", m);
69+
70+
m:=IdentityMat(dim,GF(2));; ConvertToMatrixRep(m);;
71+
RunMatTest("GF(2) compressed matrix", m);
72+
73+
m:=IdentityMat(dim,GF(7));;
74+
RunMatTest("GF(7) rowlist", m);
75+
76+
m:=IdentityMat(dim,GF(7));; ConvertToMatrixRep(m);;
77+
RunMatTest("GF(7) compressed matrix", m);
78+
79+
# TODO: also add cvec matrices
80+
od;

0 commit comments

Comments
 (0)