Two Questions on R (call by reference and pre-compilation)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Two Questions on R (call by reference and pre-compilation)

Ruihong Huang
Hi All,

I have two questions on R. Could you please explain them to me? Thank you!

1) When call a function, R typically copys the values to formal
arguments (call by value). This is very cost, if I would like to pass a
huge data set to a function. Is there any situations that R doesn't copy
the data, besides pass data in an environment object.

2) Does R pre-compile the object function to binary when running
"optim"? I experienced the R "optim" is much slower than the MATLAB
"fmincon" function. I don't know MATLAB has done any pre-compilation on
the script for object function or not. But perhaps, we can increase R
performance by some sort of pre-compilation during running time.


Thanks in advance.


Best Regards,
Ruihong

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Two Questions on R (call by reference and pre-compilation)

Steve Lianoglou-6
Hi,

On Tue, May 4, 2010 at 5:05 PM, Ruihong Huang
<[hidden email]> wrote:
> Hi All,
>
> I have two questions on R. Could you please explain them to me? Thank you!
>
> 1) When call a function, R typically copys the values to formal arguments
> (call by value).

This is technically incorrect.

As far as I know, R has "copy-on-write" semantics. It will only make a
copy of the passed in object if you modify it within your function.

> This is very cost, if I would like to pass a huge data set
> to a function. Is there any situations that R doesn't copy the data, besides
> pass data in an environment object.

This question comes up quite often, you could try searching the
archives to get more info about that (using gmane might be helpful).

Check out this SO thread as well:
http://stackoverflow.com/questions/2603184/r-pass-by-reference

> 2) Does R pre-compile the object function to binary when running "optim"? I
> experienced the R "optim" is much slower than the MATLAB "fmincon" function.
> I don't know MATLAB has done any pre-compilation on the script for object
> function or not. But perhaps, we can increase R performance by some sort of
> pre-compilation during running time.

If I had to guess, I'd guess that it doesn't, but let's see what the
gurus say ...

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Two Questions on R (call by reference and pre-compilation)

Duncan Murdoch-2
In reply to this post by Ruihong Huang
On 04/05/2010 5:05 PM, Ruihong Huang wrote:
> Hi All,
>
> I have two questions on R. Could you please explain them to me? Thank you!
>
> 1) When call a function, R typically copys the values to formal
> arguments (call by value). This is very cost, if I would like to pass a
> huge data set to a function. Is there any situations that R doesn't copy
> the data, besides pass data in an environment object.
>  

R doesn't copy data unless it needs to, for example if your function
modifies its copy.  So don't worry about the cost, there usually isn't
much of one.
> 2) Does R pre-compile the object function to binary when running
> "optim"? I experienced the R "optim" is much slower than the MATLAB
> "fmincon" function. I don't know MATLAB has done any pre-compilation on
> the script for object function or not. But perhaps, we can increase R
> performance by some sort of pre-compilation during running time.
>  

There's an experimental compiler, but I don't know if there's a
predicted release date for it.  R is not an easy language to compile.

Duncan Murdoch

>
> Thanks in advance.
>
>
> Best Regards,
> Ruihong
>  
> ------------------------------------------------------------------------
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Two Questions on R (call by reference and pre-compilation)

Ruihong Huang
In reply to this post by Steve Lianoglou-6

Thank all of you!

On 05/05/2010 12:08 AM, Steve Lianoglou wrote:

> Hi,
>
> On Tue, May 4, 2010 at 5:05 PM, Ruihong Huang
> <[hidden email]>  wrote:
>    
>> Hi All,
>>
>> I have two questions on R. Could you please explain them to me? Thank you!
>>
>> 1) When call a function, R typically copys the values to formal arguments
>> (call by value).
>>      
> This is technically incorrect.
>
> As far as I know, R has "copy-on-write" semantics. It will only make a
> copy of the passed in object if you modify it within your function.
>
>    
>> This is very cost, if I would like to pass a huge data set
>> to a function. Is there any situations that R doesn't copy the data, besides
>> pass data in an environment object.
>>      
> This question comes up quite often, you could try searching the
> archives to get more info about that (using gmane might be helpful).
>
> Check out this SO thread as well:
> http://stackoverflow.com/questions/2603184/r-pass-by-reference
>
>    
>> 2) Does R pre-compile the object function to binary when running "optim"? I
>> experienced the R "optim" is much slower than the MATLAB "fmincon" function.
>> I don't know MATLAB has done any pre-compilation on the script for object
>> function or not. But perhaps, we can increase R performance by some sort of
>> pre-compilation during running time.
>>      
> If I had to guess, I'd guess that it doesn't, but let's see what the
> gurus say ...
>
>    

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Two Questions on R (call by reference and pre-compilation)

Seth
In reply to this post by Ruihong Huang
As far as large data sets, I've just discovered readLines and writeLines functions.  I'm using it now to read in single rows, calculate things on them, and then write a single row to a file.