Wee-Jin, The other option that you have is to set up your function as an expression and then evaluate the expression for each new value of x. This might be faster in some cases.
sigmoid.fun <- function(x) 1/(1+exp(-x)) sigmoid.expr <- expression( 1/(1+exp(-x)) ) x <- runif(10^6) # non-vectorized function system.time( for(i in seq(along=x)) sigmoid.fun(x[i]) ) [1] 6.76 0.01 7.13 NA NA # vectorized function system.time( sigmoid.fun(x) ) [1] 0.56 0.00 0.59 NA NA # vectorized expression system.time( eval(sigmoid.expr) ) [1] 0.37 0.01 0.39 NA NA -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Wee-Jin Goh Sent: Sunday, November 19, 2006 4:26 PM To: r-help@stat.math.ethz.ch Subject: [R] Speeding up small functions Greetings list, In my code, I have a few small functions that are called very very frequently. An example of one such function is the following : sigmoid<-function(x) 1/(1+exp(-x)) Now, is there anyway to make this go faster? For example, in C++ we could make it inline. Is there a corresponding feature in R? Cheers, Wee-Jin ______________________________________________ R-help@stat.math.ethz.ch 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. ______________________________________________ R-help@stat.math.ethz.ch 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.