Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Efficiency of the colon operator in Mata


From   "Ben Jann" <[email protected]>
To   <[email protected]>
Subject   st: Efficiency of the colon operator in Mata
Date   Mon, 22 May 2006 15:09:49 +0200

Hi, I have an expression such as

 y = x :* k

in my Mata code, where x is vector and k is a vector or a scalar
depending on context. I am now wondering whether it is reasonable to
avoid using the colon operator in cases where k is a scalar and, e.g.,
code

 if (length(k)==1) y = x * k
 else              y = x :* k

I did some speed tests and they are indicating that using the second
solution will be considerably faster in case of k being scalar. Here is
an example:

. mata: x = uniform(10000,1)
r; t=0.00 15:08:55

. mata: for (i=1;i<=10000;i++) y = x*1
r; t=2.20 15:08:57

. mata: for (i=1;i<=10000;i++) y = x:*1
r; t=4.80 15:09:02

Can anyone confirm these results? Is it true that the colon operator
significantly slows down computations if used in situations where it is
not really needed or is this just an artifact produced by my local
machine?

ben

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index