Skip to content

Commit

Permalink
Add old MatrixObj benchmarks
Browse files Browse the repository at this point in the history
I wrote these years ago. I think it's useful to have them in the
repo, plus we can now build further benchmarks atop of this.
  • Loading branch information
fingolfin committed Nov 24, 2022
1 parent 2266634 commit e1c3728
Show file tree
Hide file tree
Showing 3 changed files with 402 additions and 0 deletions.
72 changes: 72 additions & 0 deletions benchmark/matobj/GAP-double-index-mat-access.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Two hacks


#InstallMethod( \[\], [ IsMatrix, IsList ], {m,l} -> m[l[1]][l[2]] );
InstallOtherMethod( \[\], [ IsMatrix, IsPosInt, IsPosInt ], {m,i,j} -> m[i][j] );
InstallOtherMethod( MatElm, [IsMatrix,IsPosInt,IsPosInt], {m,i,j} -> m[i][j] );


m:=List([1..10],i->List([1..10],j-> (i-1)*10 + j));


gap> m:=IdentityMat(10);;
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;
62
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;
596
gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;

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;
525
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
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;
54

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;
54
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;
63


gap> m:=IdentityMat(10,GF(2));;
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;
68
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;
468
gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;

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;
379
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
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;
135

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;
81


gap> m:=IdentityMat(10,GF(2));; ConvertToMatrixRep(m);;
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;
152 # note: SLOWER
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;
354
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;
227
gap> f:=ApplicableMethod(MatElm, [m,i,j]);;
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;
224


gap> for i in [1..10] do for j in [1..10] do Assert(0, m[i,j] = m[i][j]); od; od;
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;


InstallOtherMethod( \[\], [ IsGF2MatrixRep, IsPosInt, IsPosInt ], MAT_ELM_GF2MAT );
InstallMethod( MatElm, [ IsGF2MatrixRep, IsPosInt, IsPosInt ], MAT_ELM_GF2MAT );

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;
60
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;
65

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;
185 changes: 185 additions & 0 deletions benchmark/matobj/bench-mat.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
ReadGapRoot("benchmark/matobj/bench.g");

PrintBoxed := function(str)
local n, line;
n := Length(str) + 2;
line := Concatenation("+", ListWithIdenticalEntries(n,'-'), "+\n");
Print(line);
Print("| ", str, " |\n");
Print(line);
end;

PrintHeadline := function(what);
Print(TextAttr.5, "Testing ", what, ":\n",TextAttr.reset);
end;

MyBench := function(func)
local opt, res;

opt := rec(
mintime:=300,
showSummary := false
);
res := Benchmark(func, opt);

Print(" ", Round(res.counter * 1000.0 / res.total ), " iterations per second\n");

# Print(" Performed ", res.counter, " iterations, taking ", Round(res.total), " milliseconds; ");
# #Print("timings: ", timings, "\n");
# # Print("average: ", Round(100*res.avg)/100,
# # " +/- ", Round(100*res.std)/100,
# # " (+/- ", Round(100*res.std/res.avg), "%)",
# # "\n");
# Print(" median: ", res.median, "\n");
end;


TestReadingMatrix := function(m)
local f;

PrintHeadline("m[i][j]");
MyBench(function()
local u, i, j, rows, cols, x;
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
x:=m[i][j];
od;
od;
od;
end);

PrintHeadline("m[i,j]");
MyBench(function()
local u, i, j, rows, cols, x;
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
x:=m[i,j];
od;
od;
od;
end);

PrintHeadline("MatElm(m,i,j)");
MyBench(function()
local u, i, j, rows, cols, x;
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
x:=MatElm(m,i,j);
od;
od;
od;
end);

PrintHeadline("MatElm(m,i,j) with prefetched method");
f:=ApplicableMethod(MatElm, [m,1,1]);;
MyBench(function()
local u, i, j, rows, cols, x;
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
x:=f(m,i,j);
od;
od;
od;
end);

end;

TestWritingMatrix := function(m)
local f;

PrintHeadline("m[i][j]:=elm");
MyBench(function()
local u, i, j, rows, cols, x;
x:=m[1][1];
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
m[i][j]:=x;
od;
od;
od;
end);

PrintHeadline("m[i,j]:=elm");
MyBench(function()
local u, i, j, rows, cols, x;
x:=m[1][1];
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
m[i,j]:=x;
od;
od;
od;
end);

PrintHeadline("SetMatElm(m,i,j,elm)");
MyBench(function()
local u, i, j, rows, cols, x;
x:=m[1][1];
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
SetMatElm(m,i,j,x);
od;
od;
od;
end);

PrintHeadline("SetMatElm(m,i,j,elm) with prefetched method");
f:=ApplicableMethod(SetMatElm, [m,1,1,m[1][1]]);;
MyBench(function()
local u, i, j, rows, cols, x;
x:=m[1][1];
rows := [1..Length(m)];
cols := [1..Length(m[1])];
for u in [1..QuoInt(100000,Length(m)*Length(m[1]))] do
for i in rows do
for j in cols do
f(m,i,j,x);
od;
od;
od;
end);
end;

RunMatTest := function(desc, m)
Print("\n");
PrintBoxed(Concatenation("Testing ", desc));
TestReadingMatrix(m);
Print(TextAttr.2, "...now testing write access...\n", TextAttr.reset);
TestWritingMatrix(m);
end;

m:=IdentityMat(10);;
RunMatTest("integer matrix", m);

m:=IdentityMat(10,GF(2));;
RunMatTest("GF(2) rowlist", m);

m:=IdentityMat(10,GF(2));; ConvertToMatrixRep(m);;
RunMatTest("GF(2) compressed matrix", m);

m:=IdentityMat(10,GF(7));;
RunMatTest("GF(7) rowlist", m);

m:=IdentityMat(10,GF(7));; ConvertToMatrixRep(m);;
RunMatTest("GF(7) compressed matrix", m);
Loading

0 comments on commit e1c3728

Please sign in to comment.