On 06-May-2014 18:09:12 Göran Broström wrote: > A thread on r-devel ("Historical NA question") went (finally) off-topic, > heading towards "Precedence". This triggered a question that I think is > better put on this list: > > I have been more or less regularly been writing programs since the > seventies (Fortran, later C) and I early got the habit of using > parentheses almost everywhere, for two reasons. The first is the > obvious, to avoid mistakes with precedences, but the second is almost > as important: Readability. > > Now, I think I have seen somewhere that unnecessary parentheses in R > functions may slow down execution time considerably. Is this really > true, ant should I consequently get rid of my faiblesse for parentheses? > Or are there rules for when it matters and doesn't matter? > > Thanks, Göran
I have much sympathy with your motives for using parentheses! (And I have a similar computing history). My general encouragement would be: continue using them when you feel that each usage brings a benefit. Of course, you would avoid silliness like x <- (1*(2*(3*(4*(5))))) and indeed, that does slow it down somewhat (it takes about twice as long as a <- 1*2*3*4*5): system.time(for(i in (1:10000)) 1*2*3*4*5) # user system elapsed # 0.032 0.000 0.032 system.time(for(i in (1:10000)) (1*(2*(3*(4*(5)))))) # user system elapsed # 0.056 0.000 0.054 The main reason, I suppose, is that a "(" forces a new level on a stack, which is not popped until the matching ")" arrives. Interestingly, the above silliness takes a little longer when the nesting is the other way round: system.time(for(i in (1:10000)) 1*2*3*4*5) # user system elapsed # 0.028 0.000 0.029 system.time(for(i in (1:10000)) (((((1)*2)*3)*4)*5) ) # user system elapsed # 0.052 0.000 0.081 (though in fact the times are somwhat variable in both cases, so I'm not sure of the value of the relationship). Best wishes, Ted. ------------------------------------------------- E-Mail: (Ted Harding) <ted.hard...@wlandres.net> Date: 06-May-2014 Time: 19:41:13 This message was sent by XFMail ______________________________________________ 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.