[R] loops and simulation

David Winsemius dwinsemius at comcast.net
Thu Jul 21 07:43:57 CEST 2011


On Jul 21, 2011, at 1:04 AM, Daniel Malter wrote:

> http://mlg.eng.cam.ac.uk/dave/rmbenchmark.php
>
> I haven't ever tried it myself, but online sources suggest that  
> Matlab possibly gains speed by internally avoiding loops rather than  
> looping faster. What would stand at the end if this were true,  
> however, is improved end user speed.

When I ran the Toeplitz matrix creation test on a 3 year-old Mac, not  
the fastest available at the time, inside their 20 run test with the  
outer() function I get:
---------
b <- outer(j, k, function(j,k)  abs(j - k) + 1)

Creation of a 220x220 Toeplitz matrix (loops)_______ (sec):  0.0034
-------------
When I run their code I get a number very similar to theirs:

-------
  for (j in 1:220) {
       for (k in 1:220) {
          b[k,j] <- abs(j - k) + 1
        }
      }

Creation of a 220x220 Toeplitz matrix (loops)_______ (sec):  0.2338
-----------

So I guess that suggests that either the loop construct or the 220 x  
220 assignments  are the holdup  since the calculation and single  
assignment don't take much time.

I was thinking you were comparing loops to *apply strategies, but I  
guess your comparison was different.

-- 
David.

>
> Daniel
> ________________________________________
> From: David Winsemius [dwinsemius at comcast.net]
> Sent: Wednesday, July 20, 2011 9:01 AM
> To: Daniel Malter
> Cc: r-help at r-project.org
> Subject: Re: [R] loops and simulation
>
> On Jul 20, 2011, at 1:34 AM, Daniel Malter wrote:
>
> snipped
>> requests, except that you were referring to SAS and had heard that R
>> "does
>> not like loops." (This is factually wrong. But R can be slow  
>> looping).
>
> Where did you hear this? Can you cites any references?
>
> --
>
> David Winsemius, MD
> West Hartford, CT
>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list