-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add benchmarks to test matrix changes
- Loading branch information
Showing
3 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
ReadGapRoot("bench.g"); | ||
|
||
# TODO: the following helper functions are meant to allow writing | ||
# code that uses (Set)MatElm but still accepts "old-style" matrices. | ||
# as input. We might want to add them to the library... or maybe not, | ||
# as new code probably should use mat[i,j] directly instaed. | ||
InstallOtherMethod( MatElm, [ IsMatrix, IsPosInt, IsPosInt ], | ||
-RankFilter(IsMatrix), | ||
{m,i,j} -> m[i,j] ); | ||
InstallOtherMethod( SetMatElm, [ IsMatrix, IsPosInt, IsPosInt, IsObject ], | ||
-RankFilter(IsMatrix), | ||
function(m,i,j,v) m[i,j]:=v; end); | ||
|
||
# TODO: add tests for all of this | ||
# TODO: also test Unbind(mat[i,j]) / IsBound(mat[i,j]) ?!? then again, we might not want | ||
# to support these at all, given that matrix objects should be dense | ||
# TODO: also add tests for out-of-bounds array indices | ||
|
||
|
||
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); |
Oops, something went wrong.