I run this on my computer in Stata 13.1:
The first loop takes 30.38 seconds and the second takes 0.04 seconds, a ratio of 760. If I change
to
, the difference largely disappears.
The practical situation is that I am running -simulate- on what is essentially a Mata function. My -simulate- command line calls a little Stata program, which in turn runs the Mata function, then saves its results in r() macros to pass back to -simulate-. I was surprised to discover that assignments rather like this were dominating my run time and causing 100,000 simulations to take far longer than they should.
UPDATE: I just tried this another computer, and there was no problem. Then I opened a fresh instance of Stata on the original computer and again there was no problem. So there is something about the current environment in my current Stata instance that is happening as I run the simulations that is causing this massive slowdown.... Here is the log:
Code:
mata t = 1 forvalues i=1/1000 { mata r=t } mata for (i=1000;i;i--) r=t
Code:
mata r=t
Code:
mata r=1
The practical situation is that I am running -simulate- on what is essentially a Mata function. My -simulate- command line calls a little Stata program, which in turn runs the Mata function, then saves its results in r() macros to pass back to -simulate-. I was surprised to discover that assignments rather like this were dominating my run time and causing 100,000 simulations to take far longer than they should.
UPDATE: I just tried this another computer, and there was no problem. Then I opened a fresh instance of Stata on the original computer and again there was no problem. So there is something about the current environment in my current Stata instance that is happening as I run the simulations that is causing this massive slowdown.... Here is the log:
Code:
. mata t = 1 r; t=0.03 8:53:19 . forvalues i=1/1000 { 2. mata r=t 3. } r; t=30.19 8:53:49 . . mata for (i=1000;i;i--) r=t r; t=0.04 8:53:49 . end of do-file r; t=30.38 8:53:49 . di 30.38/.04 759.5 r; t=0.00 8:54:29 . do "C:\Users\David\AppData\Local\Temp\STD03000000.tmp" . mata t = 1 r; t=0.05 8:55:04 . forvalues i=1/1000 { 2. mata r=1 3. } r; t=0.33 8:55:04 . . mata for (i=1000;i;i--) r=t r; t=0.04 8:55:04
Comment