Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Efficiency: was RE: Taking averages, etc.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Efficiency: was RE: Taking averages, etc.
Date   Wed, 17 Dec 2003 13:11:12 -0000

Allan Reese

> While it is possible to replace one value in a variable 
> (say the 4th)
> using:
> 
>   replace  x = y if _n == 4
> 
> that code tests _n for every element.  It's very wasteful, 
> though you may
> not notice in most interactive sessions.  In a program however, the
> alternative
> 
>   replace  x = y in 4
> 
> will be about _N times faster.  As this will typically be 
> inside a loop
> dealing will each element of the variable, it may reduce 
> execution time
> from O(_N squared) to O(_N).

This is an old song, but it deserves a replay.

There is one side-issue. Allan says 
"this will typically be inside a loop
dealing will each element of the variable". 

In Stata, that's not typical at all. Looping 
explicitly over observations is sometimes 
needed, but in the majority of cases it 
is a sign that you've not seen the Stata way 
to do something. 

Nick 
[email protected] 
*
*   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