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,
Hi, 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?There shouldn't be, but you can use system.time() to discover whether that is true for your particular function. Regardless, I would think that the savings in programmer time and later readability far outweighs any miniscule savings in run time. Sarah -- Sarah Goslee http://www.functionaldiversity.org
Hi Christofer, Any speed reduction should be relatively small, and the advantage in terms of easily maintainable, clearly understandable code will be enormous. If the function you make from the code does not have any methods and you are concerned about a slow down related to your loop repeatedly calling the function, I would would do something like: require(compiler) foo <- function() {your user code} fooloop <- function() {your for loop {foo()}} fooloop <- cmpfun(fooloop) which will byte compile your fooloop function. Cheers, Josh On Thu, Nov 10, 2011 at 7: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. >-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, ATS Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
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. >
You can always runs a quick test. Here a loop is repeated 10M times and the cost of the function call adds 344 ns; is this significant in your case? Probably not for the modularity that is gained. For your 10,000 loops, it would add 3.44 ms to the overall run (less than an blink of the eye).> iter <- 10000000 # number of iteration > > system.time(for (i in 1:iter) {})user system elapsed 2.22 0.02 2.05> system.time(for (i in 1:iter) f.null())user system elapsed 5.66 0.00 5.31> > 5.66 / iter[1] 0.000000566> (5.66 - 2.22)/ iter[1] 0.000000344>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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Christofer Bogaso <bogaso.christofer <at> gmail.com> writes: [...]> 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?[...] You did not asked for it, you may know it, or may not know it: if you use apply functions and other vector oriented functions, this can bring you a huge speedup, compared to a for-loop. Ciao, Oliver