Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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

From: Ping Cao

Date: 19 Nov, 2010 03:41:04

Message: 1 of 8

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.

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

From: Walter Roberson

Date: 19 Nov, 2010 04:03:25

Message: 2 of 8

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 .

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

From: Ping Cao

Date: 19 Nov, 2010 08:00:06

Message: 3 of 8

Walter Roberson <roberson@hushmail.com> wrote in message <hGmFo.55406$d67.34844@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?

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

From: Rune Allnor

Date: 19 Nov, 2010 08:12:54

Message: 4 of 8

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

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

From: Bruno Luong

Date: 19 Nov, 2010 08:15:05

Message: 5 of 8

"Ping Cao" <cpa841004@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

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

From: Bruno Luong

Date: 19 Nov, 2010 08:20:08

Message: 6 of 8

Rune Allnor <allnor@tele.ntnu.no> wrote in message <a19914fd-b2f2-45b9-848a-ad7868ddf278@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

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

From: Rune Allnor

Date: 19 Nov, 2010 09:04:10

Message: 7 of 8

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

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

From: Bruno Luong

Date: 19 Nov, 2010 09:37:07

Message: 8 of 8

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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us