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

New to MATLAB?

Thread Subject:
What is the resolution of your tic/toc?

Subject: What is the resolution of your tic/toc?

From: Teja Muppirala

Date: 24 Sep, 2009 23:06:04

Message: 1 of 1

When using tic and toc, I have sometimes wondered what the finest time interval toc can measure is. The following script is one attempt to figure that out:

x = [];
for k = 1:1e5
   x = min([-(toc-toc) x]);
format long

Running the following code on my 2 computers gives me
x = 1.676190689026136e-006 on my fast computer
x = 2.234920918664509e-006 on my slightly slower computer

So basically around microsecond order. The interesting thing is, no matter how many times I run it, I get exactly the same answer. Just curious, but what kind of answers do you guys get when you run this?

By the way, I find that you can make fantastically accurate timed loops by doing things like:

N = 0;
TIMESTEP= 1/60; % 60Hz
NEXT = 0;
while (...)
   if toc > NEXT
      N = N+1;
      NEXT = TIMESTEP * N;
      (do something)

Tags for 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