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