[R] Meaning of proc.time()

William Dunlap wdunlap at tibco.com
Thu Jul 29 17:51:44 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Christofer Bogaso
> Sent: Thursday, July 29, 2010 7:51 AM
> To: jim holtman
> Cc: R-help at stat.math.ethz.ch
> Subject: Re: [R] Meaning of proc.time()
> 
> Ok, but what are "user CPU" and "system CPU?" If I know corrrectly, I
> only have a single CPU in my system. It is not a multi-corer system.
> Should I read that, system CPU time is the time that my CPU took for
> actual calculation, and the user CPU time is the time that my CPU took
> to analyze my code that I have written in the console?

"User CPU time" gives the CPU time spent by the current
process (i.e., the current R session) and "system CPU time"
gives the CPU time spent by the kernel (the operating
system) on behalf of the current process.  The operating
system is used for things like opening files, doing
input or output, starting other processes, and looking at
the system clock:  operations that involve resources that many
processes must share.  Different operating systems will
have different things done by the operating system.

E.g., look at the difference between opening a file once
per write for many writes and opening it just once:

> system.time(for(i in 1:10^4)cat(i, file="e:/temp/i.txt", append=TRUE))
   user  system elapsed 
   1.00    2.92    4.08 
> system.time({file<-file("e:/temp/i.txt","w");for(i in 1:10^4)cat(i, file=file);close(file)})
   user  system elapsed 
   0.10    0.08    0.15

Most of the time you are not terribly interested in whether
the CPU is being used by R or by the operating system on
R's behalf.  However, a large system time can point towards
problems in not batching up I/O or other OS requests enough.   

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> On Tue, Jul 27, 2010 at 4:12 PM, jim holtman 
> <jholtman at gmail.com> wrote:
> > This is reporting on the accumulated values of the user CPU, system
> > CPU and elapsed time.  The 'user + system' values indicate how much
> > CPU has been used to process whatever has occurred in the R 
> session so
> > far.  To get the time to execute a statement, or block of code, you
> > can use 'system.time' or take the difference in two values of
> > proc.time:
> >
> >> a <- proc.time()
> >> for(i in 1:1e6) x <- 1
> >> proc.time() - a  # elapsed
> >   user  system elapsed
> >   0.38    0.05   42.48
> >>
> >
> > Here is some stuff I typed in at the console.  It took me 42 seconds
> > (elapsed or wall clock time) to type in the commands to the GUI.  I
> > executed a simple 'for' loop for 1,000,000 iteration and 
> this required
> > 0.43 CPU seconds (user + system).
> >
> >> a <- proc.time()
> >> for(i in 1:1e7) x <- 1
> >> proc.time() - a  # elapsed
> >   user  system elapsed
> >   3.33    0.03   18.14
> >>
> >
> > Notice that I executed the 'for' loop 10,000,000 times and the CPU
> > time is now 3.36 seconds, or about 10X more than the first one which
> > is about what you would expect.   This is a common way of 
> determining
> > what portions of your script are taking the most resource.
> >
> > HTH
> >
> > On Tue, Jul 27, 2010 at 4:03 AM, Christofer Bogaso
> > <bogaso.christofer at gmail.com> wrote:
> >> If I run proc.time() function, I would get following:
> >>
> >>> proc.time()
> >>    user  system elapsed
> >>    2.82    4.18  792.39
> >>
> >> However I am struggling the meaning of the object what it 
> returned. In
> >> help file it says that:
> >>
> >> "user time" is the time required to execute the calling 
> process. Here
> >> what is the calling process exactly? Who executes this? 
> system itself?
> >>
> >> 2ndly "system time" is the time required by the system for 
> execution
> >> of something.......... what execution? What is the meaning of " on
> >> behalf of the
> >>      calling process?"
> >>
> >> 3rdly what is elapsed time? Total time I spent on current session,
> >> since I opened the R window?
> >>
> >> Really sorry if I asked trivial questions, however honestly I could
> >> not understand those. Some helpful comments are highly appreciated.
> >>
> >> Thanks,
> >>
> >> ______________________________________________
> >> R-help at 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.
> >>
> >
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem that you are trying to solve?
> >
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list