tag:blogger.com,1999:blog-24082283.post3406773275302465000..comments2024-02-07T05:26:45.500-05:00Comments on Yu-Sung Su's Blog: speed issue in R computing: apply() vs a loopYu-Sung Suhttp://www.blogger.com/profile/16723440380040533242noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-24082283.post-70128671399341838012008-07-15T03:04:00.000-04:002008-07-15T03:04:00.000-04:00There was a time when the apply family of function...There was a time when the apply family of functions was a lot faster, but that's no longer true. These days, performance is usually equivalent. Me, I use them no matter what, since the code is a lot cleaner.<BR/><BR/>That said, in the special case where apply works (lapply almost always works, but row- or columnwise seems a special case), it is faster in my experience. Try something real-worldwcwhttps://www.blogger.com/profile/16307608293310560164noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-16447896019008489842008-04-25T02:58:00.000-04:002008-04-25T02:58:00.000-04:00Oops, my fault!But at least we reach a conclusion ...Oops, my fault!<BR/><BR/>But at least we reach a conclusion that loop is not slower if not faster than apply().Yu-Sung Suhttps://www.blogger.com/profile/16723440380040533242noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-40128050372519345412008-04-25T02:55:00.000-04:002008-04-25T02:55:00.000-04:00I really wonder how you can let applytest2.R run c...I really wonder how you can let applytest2.R run correctly without specifying the "temp" variable in FUN.loop and generate any results. <BR/><BR/>Besides, please note that length(b)=10,000 but you were using 1,000 loops for the for function. <BR/><BR/>The performances will at least equal after the update.<BR/><BR/>play <- function(a){<BR/>+ exp(a)+sin(a)+rnorm(1)<BR/>+ }<BR/>> b <- rnorm(10000)Hesen Penghttps://www.blogger.com/profile/04343795657339548587noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-64578832914808275442008-04-24T22:57:00.000-04:002008-04-24T22:57:00.000-04:00Hi,I don't know why but I feel your examples are n...Hi,<BR/><BR/>I don't know why but I feel your examples are not comparable.<BR/><BR/>I revise your code as:<BR/>http://yusung.googlepages.com/applytest2.R<BR/><BR/>Now loop is faster than apply().<BR/><BR/>As for Prof. Ripley's comment, see my original post and look for here is the result. There is a hyper link for the word "result".Yu-Sung Suhttps://www.blogger.com/profile/16723440380040533242noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-25579216185954743802008-04-24T22:07:00.000-04:002008-04-24T22:07:00.000-04:00Hey,A more comprehensive study is done here. http:...Hey,<BR/><BR/>A more comprehensive study is done here. http://hesen.peng.googlepages.com/haha.r<BR/><BR/>I'm very interested in the problem. Could you please give me the link to what Professor Brian Ripley said in your original post? Thank you.Hesen Penghttps://www.blogger.com/profile/04343795657339548587noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-25672616084091631252008-04-24T13:29:00.000-04:002008-04-24T13:29:00.000-04:00Hi,I have compared apply() vs sapply(). sapply() ...Hi,<BR/><BR/>I have compared apply() vs sapply(). sapply() is no faster than apply(). I guess for your example, maybe you should do a MC test (see my example). I guess a loop is no slower if not faster than apply().Yu-Sung Suhttps://www.blogger.com/profile/16723440380040533242noreply@blogger.comtag:blogger.com,1999:blog-24082283.post-62720672183574536502008-04-24T04:29:00.000-04:002008-04-24T04:29:00.000-04:00Hi,But if you try this you can find that sapply() ...Hi,<BR/><BR/>But if you try this you can find that sapply() run it a little faster.<BR/><BR/>> play <- function(x){<BR/>+ return(exp(x)+sin(x)-rnorm(1))<BR/>+ }<BR/>> <BR/>> b <- rnorm(1000)<BR/>> <BR/>> time <- proc.time()<BR/>> temp <- sapply(b,FUN="play")<BR/>> print(proc.time()-time)<BR/> user system elapsed <BR/> 0.032 0.000 0.208 <BR/>> <BR/>> <BR/>> time <- proc.time()<BR/>> tempHesen Penghttps://www.blogger.com/profile/04343795657339548587noreply@blogger.com