Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's the difference between cputime and tic, toc?

464 views
Skip to first unread message

Ping Cao

unread,
Nov 18, 2010, 10:41:04 PM11/18/10
to
Hello, everyone.

I want to record how much time Matlab uses while running a programming. If I use cputime, the result is 94.28 while the result of using tic, toc is 28.02. I feel that 28.02 is more accurate.

So I'm wondering what's the difference between cputime and tic, toc.

Thanks in advance.

Walter Roberson

unread,
Nov 18, 2010, 11:03:25 PM11/18/10
to
On 18/11/10 9:41 PM, Ping Cao wrote:

> I want to record how much time Matlab uses while running a programming.
> If I use cputime, the result is 94.28 while the result of using tic, toc
> is 28.02. I feel that 28.02 is more accurate.
> So I'm wondering what's the difference between cputime and tic, toc.

"cputime returns the total CPU time (in seconds) used by your MATLAB
application from the time it was started."

On the other hand, toc returns the time since the last tic .

Ping Cao

unread,
Nov 19, 2010, 3:00:06 AM11/19/10
to
Walter Roberson <robe...@hushmail.com> wrote in message <hGmFo.55406$d67....@newsfe16.iad>...

So CPU time is larger than the elapsed real time?

Rune Allnor

unread,
Nov 19, 2010, 3:12:54 AM11/19/10
to
On Nov 19, 9:00 am, "Ping Cao" <cpa841...@163.com> wrote:
> Walter Roberson <rober...@hushmail.com> wrote in message <hGmFo.55406$d67.34...@newsfe16.iad>...

> > On 18/11/10 9:41 PM, Ping Cao wrote:
>
> > > I want to record how much time Matlab uses while running a programming.
> > > If I use cputime, the result is 94.28 while the result of using tic, toc
> > > is 28.02. I feel that 28.02 is more accurate.
> > > So I'm wondering what's the difference between cputime and tic, toc.
>
> > "cputime returns the total CPU time (in seconds) used by your MATLAB
> > application from the time it was started."
>
> > On the other hand, toc returns the time since the last tic .
>
> So CPU time is larger than the elapsed real time?

Historically, the feirst versions of TIC/TOC measured
the elapsed time, while CPUTIME could be used to measure
the time the CPU spent on a particular task:

tstart = cputime;
doSomething;
tSpentOnDoingSomething = cputime - tstart;

Over time, multithread CPUs became more and more
widespread and somebody realized that no one cares
for elapsed time - it varies with all kinds of
random factors across the system. So TIC/TOC was
changed to instead display the time the CPU speds
on the task.

Rune

Bruno Luong

unread,
Nov 19, 2010, 3:15:05 AM11/19/10
to
"Ping Cao" <cpa8...@163.com> wrote in message <ic5aq6$6i2$1...@fred.mathworks.com>...

>
> So CPU time is larger than the elapsed real time?

Possibly with multicore CPU. But please don't take literally my world on that.

Bruno

Bruno Luong

unread,
Nov 19, 2010, 3:20:08 AM11/19/10
to
Rune Allnor <all...@tele.ntnu.no> wrote in message <a19914fd-b2f2-45b9...@k30g2000vbn.googlegroups.com>...

>
>
> Over time, multithread CPUs became more and more
> widespread and somebody realized that no one cares
> for elapsed time - it varies with all kinds of
> random factors across the system. So TIC/TOC was
> changed to instead display the time the CPU speds
> on the task.

Not that I'm aware of, tic/toc still displays the elapsed time, not CPU.

Bruno

Rune Allnor

unread,
Nov 19, 2010, 4:04:10 AM11/19/10
to
On Nov 19, 9:20 am, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <a19914fd-b2f2-45b9-848a-ad7868ddf...@k30g2000vbn.googlegroups.com>...

>
> > Over time, multithread CPUs became more and more
> > widespread and somebody realized that no one cares
> > for elapsed time - it varies with all kinds of
> > random factors across the system. So TIC/TOC was
> > changed to instead display the time the CPU speds
> > on the task.
>
> Not that I'm aware of, tic/toc still displays the elapsed time, not CPU.
>
> Bruno

I remember somebody fom TMW fiercly correcting me when I said
that, some time ago:

http://groups.google.com/group/comp.soft-sys.matlab/msg/741d1e08b74ca9de

I would hope somebody from TMW will chime in and unequivocally
state what is the actual case...

Rune

Bruno Luong

unread,
Nov 19, 2010, 4:37:07 AM11/19/10
to
The you get inaccurate information from this person. Simple test of

> tic; pause(10); toc

with monitoring CPU activity shows low CPU activity, and still toc will return ~10s

Bruno

0 new messages