# NOT RUN {
   ## create function to profile
     foo <- function(N){
             preallocate<-numeric(N)
             grow<-NULL
              for(i in 1:N){
                  preallocate[i]<-N/(i+1)
                  grow<-c(grow,N/(i+1))
                 }
     }
     ## save function to a source file and reload
     dump("foo",file="foo.R")
     source("foo.R")
     ## create file to save profiler output
     tmp<-tempfile()
     ## Profile the function
     Rprof(tmp,line.profiling=TRUE)
     foo(1e4)
     Rprof(append=FALSE)
     ## Create a aprof object
     fooaprof<-aprof("foo.R",tmp)
     ## display basic information, summarize and plot the object
     fooaprof
     summary(fooaprof)
     plot(fooaprof)
     profileplot(fooaprof)
     ## To continue with memory profiling:
     ## enable memory.profiling=TRUE
     Rprof(tmp,line.profiling=TRUE,memory.profiling=TRUE)
     foo(1e4)
     Rprof(append=FALSE)
     ## Create a aprof object
     fooaprof<-aprof("foo.R",tmp)
     ## display basic information, and plot memory usage
     fooaprof
     
     plot(fooaprof)
   
# }
Run the code above in your browser using DataLab