[Rd] Speed question: passing arguments vs environment

Serguei Sokol @oko| @end|ng |rom |n@@-tou|ou@e@|r
Tue Dec 2 16:58:38 CET 2025


Do you have some publicly available code snippets where you compare 
different approaches?
It can be not exactly your problem but somewhat representative.

Best,
Serguei.

Le 02/12/2025 à 13:41, Therneau, Terry M., Ph.D. via R-devel a écrit :
> I have a complex likelihood function f() to maximize, with lots of arguments (some of which set up indexes for derivatives, for instance).
> When using something like optim(), one can pass these arguments through via its � arg, or could make the likelihood function f() live in the same environment as the main routine so they are found directly.    Is there any advantage of one versus the other wrt speed?    At the end of the day, f() may get called thousands of times in a Hamiltonian MCMC.
>
> Since R does not replicate arguments that are used in a read-only fashion, one might expect little to no penalty for having them on the call chain, unless the bookkeeping for copy-on-write is itself time consuming.
>
> Terry T.
>
>
> 	[[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list