[R] A question on Programming

R. Michael Weylandt michael.weylandt at gmail.com
Thu Nov 10 16:28:21 CET 2011


Yes, there will be some reduction in speed:

E.g.,

> system.time(replicate(1e5, (function() sum(1:10))()))
   user  system elapsed
  0.696   0.022   0.729
> system.time(replicate(1e5, sum(1:10)))
   user  system elapsed
  0.292   0.006   0.306

But it's not much: 3 tenths of a second for 10,000 calls. It's
certainly worth it for ease of readibility and debugging.

Michael

On Thu, Nov 10, 2011 at 10:07 AM, Christofer Bogaso
<bogaso.christofer at gmail.com> wrote:
> Dear all. Let say I have a group of codes which will be used in many places
> in my overall R-code files. These group of codes will be used within a
> for-loop (with a big length, like 10000 times) and also many other places
> outside of that for loop. As this group of codes are being used in many
> places, I thought to put them within a user-defined function.
>
> Here my question is, is there any speed reduction if I put them within a
> function (I think there may be some speed reduction at least within
> for-loop, because that loop needs to call that function many times),
> relative to if I used that group of codes as-it-is in many places?
>
> Thanks and regards,
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list