Hi All, Is it possible for system.time() to measure the time it takes for a machine to evaluate more than one R expression? For example, # This I can do:> system.time(x <- rnorm(100000))[1] 0.07 0.00 0.13 0.00 0.00 # But this I can't:> system.time(x <- rnorm(100000); new <- sample(x, 100000, replace=T))Error: syntax error # Nor this:> system.time(x <- rnorm(100000)+ new <- sample(x, 100000, replace=T) Error: syntax error I'm trying to compare two fairly large code chunks, so executing system.time() individually for each expression and then summing the results seems suboptimal. Have I missed something simple? Thanks in advance for any suggestions- Rob version: platform i386-pc-linux-gnu arch i386 os linux-gnu system i386, linux-gnu status major 1 minor 7.1 year 2003 month 06 day 16 language R _____________________________________________________ Rob Keefe Lab: (208) 885-5165 M.S. student Home: (208) 882-9749 University of Idaho
On Thu, Jul 24, 2003 at 07:10:27PM -0700, Robert Keefe wrote:> # This I can do: > > system.time(x <- rnorm(100000)) > [1] 0.07 0.00 0.13 0.00 0.00 > > # But this I can't: > > system.time(x <- rnorm(100000); new <- sample(x, 100000, replace=T)) > Error: syntax errorJust use curly braces:> system.time({x <- rnorm(100000); new <- sample(x, 100000, replace=T)})[1] 0.11 0.00 0.11 0.00 0.00> Have I missed something simple?See above. Another related way is to define a function; you could even use a 'throw-away anonymous' function [1]. Lastly, you probably also want to learn about profiling your code. There is an introductory article in one of the R News issues. Regards, Dirk -- Those are my principles, and if you don't like them... well, I have others. -- Groucho Marx