# RE: st: Efficiency of the colon operator in Mata

 From Joseph Coveney To Statalist Subject RE: st: Efficiency of the colon operator in Mata Date Tue, 23 May 2006 14:32:07 +0900

```Ben Jann wrote (excerpted):

I should have phrased my question differently: Is
the colon operator designed so that (almost) no computer time is lost if
it is used in an expression in which it is not really needed? From my
own timings and the timings reported by others it seems that using the
colon operator significantly slows down computations.

--------------------------------------------------------------------------------

It doesn't surprise me that a matrix operator is not optimum for performing
scalar operations.  I guess the question comes down to:  is it better for a
compiler's author to expect that programmers will use matrix operators for
matrix operations and scalar operators for scalar operations?  Or should the
compiler's author burden the matrix operator with overhead so that it can
each time detect whether the matrix operatator is being used in a scalar
operation and then switch the algorithm to one that is optimum for the
latter?  This would seem to sacrifice the matrix operator's efficiency in
its commonest use.

I don't know how often it is that a programmer anticipates that in a given
application a matrix operator will receive a scalar operand in such a
substantial proportion of the time that it affects the application's overall
speed of execution.  If it's not very common, then wouldn't it be better for
that programmer to trap those instances and re-direct that application's
program flow to a scalar operator?  Or is this circumstance so common that
it will make the majority of programmers' lives easier if the compiler's
author imbeds this detection-redirection activity into the matrix operator
to be performed at each operation?

Joseph Coveney

version 9.2
mata:
mata clear
mata set matastrict on

void function timem1()
{

real scalar i
real matrix x, y

x = (1)
y = (2)
for (i=1; (i<=100000); i++) x = x * invsym(y);
}

void function timem2()
{
real scalar i, y
real matrix x

x = (1)
y = 2
for (i=1; (i<=100000); i++) x = x / y;
}

end

set rmsg on
mata: timem1()
mata: timem2()
set rmsg off
exit

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```