Re: [R] Efficiency of for-loop in R

2007-12-27 Thread jim holtman
Exactly what is the problem you are trying to solve?  Could you
provide commented, minimal, self-contained, reproducible code?

A lot depends on what you are trying to do,  There might be other
ways, in R, than a 'for' loop to solve your problems.

On Dec 27, 2007 6:44 PM, Tong Wang [EMAIL PROTECTED] wrote:
 Hi,
   I just realized that in Matlab, as long as memory is pre-allocated, doing 
 for-loop doesn't cost more time than doing things in vector form.
   But it seems in R, it still cost a lot to do for-loop.  Is there any 
 improvement in R that I missed. Thanks a lot.

 Merry Xmas Everyone !

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Efficiency of for-loop in R

2007-12-27 Thread Tong Wang
HI,
   The question is meant to be a general one, I am trying to find out if there 
is new development in R that I might have missed. 

but here's a trivial example, 
   
To compute  y=sin(x) , x = 1,2,... 10
 x=1:10, 
1.  y =sin(x)
2.  for(i in 1:10) y=sin(x[i])

1 is much faster than 2.  
Old Matlab also had this problem, but in new versions, 1 and 2 are mostly the 
same. 
I am just wondering if the same improvement has happened or will happen to R. 


Thanks . 

 

- Original Message -
From: jim holtman [EMAIL PROTECTED]
Date: Thursday, December 27, 2007 4:39 pm
Subject: Re: [R] Efficiency of for-loop in R
To: Tong Wang [EMAIL PROTECTED]
Cc: R help [EMAIL PROTECTED]

 Exactly what is the problem you are trying to solve?  Could you
 provide commented, minimal, self-contained, reproducible code?
 
 A lot depends on what you are trying to do,  There might be other
 ways, in R, than a 'for' loop to solve your problems.
 
 On Dec 27, 2007 6:44 PM, Tong Wang [EMAIL PROTECTED] wrote:
  Hi,
I just realized that in Matlab, as long as memory is pre-
 allocated, doing for-loop doesn't cost more time than doing things 
 in vector form.
But it seems in R, it still cost a lot to do for-loop.  Is 
 there any improvement in R that I missed. Thanks a lot.
 
  Merry Xmas Everyone !
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide http://www.R-project.org/posting-
 guide.html and provide commented, minimal, self-contained, 
 reproducible code.
 
 
 
 
 -- 
 Jim Holtman
 Cincinnati, OH
 +1 513 646 9390
 
 What is the problem you are trying to solve?


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Efficiency of for-loop in R

2007-12-27 Thread jim holtman
I will venture a guess in that in the case of the 'for' loop, you are
calling 'sin' 100,000 times incurring the cost of individual function
calls at the interpreter level plus evaluating 'for' loop.  In the
vectorized case, you are only interpreting a single 'sin' call and
then internally evaluating the 'sin' function, which is a lot faster.
Also the results are different in the two cases.  In case 1) you get
10 values back in 'y' and in 2) you only get the value of the last
loop through the 'for' loop.

 system.time(for (i in 1:10) y - sin(i))
   user  system elapsed
   0.170.000.28
 str(y)
 num 0.0357
 system.time(y - sin(1:10))
   user  system elapsed
   0.010.000.02
 str(y)
 num [1:10]  0.841  0.909  0.141 -0.757 -0.959 ...


R is typically optimized for vector type operations.

On Dec 27, 2007 8:39 PM, Tong Wang [EMAIL PROTECTED] wrote:
 HI,
   The question is meant to be a general one, I am trying to find out if there 
 is new development in R that I might have missed.

 but here's a trivial example,

 To compute  y=sin(x) , x = 1,2,... 10
  x=1:10,
 1.  y =sin(x)
 2.  for(i in 1:10) y=sin(x[i])

 1 is much faster than 2.
 Old Matlab also had this problem, but in new versions, 1 and 2 are mostly the 
 same.
 I am just wondering if the same improvement has happened or will happen to R.


 Thanks .




 - Original Message -
 From: jim holtman [EMAIL PROTECTED]
 Date: Thursday, December 27, 2007 4:39 pm
 Subject: Re: [R] Efficiency of for-loop in R
 To: Tong Wang [EMAIL PROTECTED]
 Cc: R help [EMAIL PROTECTED]

  Exactly what is the problem you are trying to solve?  Could you
  provide commented, minimal, self-contained, reproducible code?
 
  A lot depends on what you are trying to do,  There might be other
  ways, in R, than a 'for' loop to solve your problems.
 
  On Dec 27, 2007 6:44 PM, Tong Wang [EMAIL PROTECTED] wrote:
   Hi,
 I just realized that in Matlab, as long as memory is pre-
  allocated, doing for-loop doesn't cost more time than doing things
  in vector form.
 But it seems in R, it still cost a lot to do for-loop.  Is
  there any improvement in R that I missed. Thanks a lot.
  
   Merry Xmas Everyone !
  
   __
   R-help@r-project.org mailing list
   https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do read the posting guide http://www.R-project.org/posting-
  guide.html and provide commented, minimal, self-contained,
  reproducible code.
  
 
 
 
  --
  Jim Holtman
  Cincinnati, OH
  +1 513 646 9390
 
  What is the problem you are trying to solve?
 




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.