Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: AW: AW: is looping slower
From 
 
"Martin Weiss" <[email protected]> 
To 
 
<[email protected]> 
Subject 
 
st: AW: AW: is looping slower 
Date 
 
Thu, 27 May 2010 13:25:31 +0200 
<> 
My test code below indicates that the -inlist()- solution is more than four
times faster than -regexm-. The -inlist()- loop takes 102 seconds, while the
-regexm- one takes 465 seconds:
*************
clear*
set mem 1G
set obs 10000000
forv i=1/9{
	gen var`i'=_n+`i'*700000
}
set rmsg on
//via -regexm()-
gen byte outcome=0
forv i=1/9{
	di in r "Now at: `i', regexm loop"
	replace outcome = 1 if regexm(string(var`i'),  /// 
	"^29")==1 |regexm(string(var`i'),  /// 
	"^30")==1 |regexm(string(var`i'), "^31")==1
}
//via -inlist()-
gen byte outcome2=0
forv i=1/9{
	di in r "Now at: `i', inlist loop"
	replace outcome2 = 1 if
inlist(substr(string(var`i'),1,2),"29","30","31") 
}
//-tostring- beforehand
tostring var1-var9, replace
gen byte outcome3=0
forv i=1/9{
	di in r "Now at: `i', tostring loop"
	replace outcome3 = 1 if inlist(substr(var`i',1,2),"29","30","31") 
}
set rmsg off
*************
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Martin Weiss
Gesendet: Donnerstag, 27. Mai 2010 09:42
An: [email protected]
Betreff: st: AW: is looping slower
<> 
Are "var1-var9" really string variables? Otherwise you will end up with a
"type mismatch" error. 
You could make your -if- qualifier shorter via -inlist()-:
*************
clear*
set obs 100000
gen myvar=_n
cou if regexm(string(myvar), "^29") |  /* 
*/ regexm(string(myvar), "^30") | /* 
*/ regexm(string(myvar), "^31") 
cou if inlist(substr(string(myvar),1,2),"29","30","31")
*************
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Jing Xia
Gesendet: Donnerstag, 27. Mai 2010 06:39
An: [email protected]
Betreff: st: is looping slower
Hello, All,
I have var1 to var9, and I'd like to create a variable outcome=1 if
any of these 9 variables satisfay condition A.
I can use a loop to loop over var1 to var9 below, but I suspect that
it takes 9 times as long if I had written it in one line:
outcome=1 if var1==conditionA | var2==conditionA | var3==conditionA |
var4==conditionA | .......var9==conditionA
Here's the loop:
gen outcome=0
foreach i of numlist 1/9 {
replace outcome = 1 if regexm(var`i', "^29")==1 |regexm(var`i',
"^30")==1 |regexm(var`i', "^31")==1
}
Is it true that looping will take much longer? (I have millions of
records) If so, is there a way to write the one line code in a concise
manner?
Thank you!
J.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/