EndPrgmEndPrgm
EndForEndFor
f2("m")→r f1(m)→r
For k,1,50For k,1,50
EndFuncEndFunc
Return #mat[1,1] Return mat[1,1]
Define f2(mat)=FuncDefine f1(mat)=Func
Local f2,k,rLocal f1,k,r
© Pass matrix by reference (name)© Pass matrix by value
PrgmPrgm
tref()tval()
The tval() routine passes the matrix m by value, and tref() passes the matrix name as a string. tval()
takes about 199 mS for each function call, while tref() takes about 31 mS. This is an improvement of
about 84%.
Even though it is faster to call a function with the reference method, it takes longer to access the matrix
elements with indirection. For some combination of function calls and matrix element accesses the
total execution time is the same for either method. This is called the break-even point. In general, the
total execution time is
T = N
c
T
c
+ N
a
T
a
where Nc and Na are respectively the number of function calls and element accesses, and Tc and Ta
are the execution times for each function call and element access. More specifically, the total execution
times for each method are
Value method
T
v
= N
cv
T
cv
+ N
av
T
av
Reference method
T
r
= N
cr
T
cr
+ N
ar
T
ar
The break-even point is at Tv = Tr. We want to compare the two methods with the same conditions, so
we equate the number of function calls and element accesses for each method:
N
cv
= N
cr
= N
c
N
av
= N
ar
= N
a
Equate the expressions for Tv and Tr:
N
c
T
cv
+ N
a
T
av
= N
c
T
cr
+ N
a
T
av
and solve for Nc: or [2]
N
c
= N
a
T
ar
−T
av
T
cv
−T
cr
N
c
N
a
=
T
ar
−T
av
T
cv
−T
cr
With equation [2], we can find the break-even point at which the two methods have the same execution
time, for some number of function calls and matrix accesses. Tcv is found with equation [1] above, and
Tcr is a constant:
T
cr
= 16.88 mS/access
Timing experiments show that the time required to access a matrix element depends on the number of
matrix rows and columns, as well as the element's location in the matrix. The access time also
changes slightly if the indices are constants or variables. Timing data shows that Tar - Tav is relatively
constant at about 5.7 mS, so equation [2] simplifies to
7 - 47