Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9219969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:14:20+00:00 2026-06-18T03:14:20+00:00

I have written some C code which I call form MATLAB after I compile

  • 0

I have written some C code which I call form MATLAB after I compile it using MEX. Inside the C code, I measure the time of a part of the computation using the following code:

clock_t begin, end;
double time_elapsed;
begin = clock();
/* do stuff... */
end = clock();
time_elapsed = (double) ((double) (end - begin) / (double) CLOCKS_PER_SEC);

Elapsed time should be the execution time in seconds.

I then output the value time_elapsed to MATLAB (it is properly exported; I checked). Then MATLAB-side I call this C function (after I compile it using MEX) and I measure its execution time using tic and toc. What turns out to be a complete absurdity is that the time I compute using tic and toc is 0.0011s (average on 500 runs, st. dev. 1.4e-4) while the time that is returned by the C code is 0.037s (average on 500 runs, st. dev. 0.0016).

Here one may notice two very strange facts:

  1. The execution time for the whole function is lower than the execution time for a part of the code. Hence, either MATLAB’s or C’s measurements are strongly inaccurate.
  2. The execution times measured in the C code are very scattered and exhibit very high st. deviation (coeff. of variation 44%, compared to just 13% for tic-toc).

What is going on with these timers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:14:21+00:00Added an answer on June 18, 2026 at 3:14 am

    You’re comparing apples to oranges.

    Look at Matlab’s documentation:

    tic – http://www.mathworks.com/help/matlab/ref/tic.html
    toc – http://www.mathworks.com/help/matlab/ref/toc.html

    tic and toc let you measure real elapsed time.

    Now look at the clock function http://linux.die.net/man/3/clock.

    In particular,

    The clock() function returns an approximation of processor time used by the program.

    The value returned is the CPU time used so far as a clock_t; to
    get the number of seconds used, divide by CLOCKS_PER_SEC. If the
    processor time used is not available or its value cannot be
    represented, the function returns the value (clock_t) -1.

    So what can account for your difference:

    • CPU time (measured by clock()) and real elapsed time (measured by tic and toc) are NOT the same. So you would expect that cpu time to be less than elapsed time? Well, maybe. What if within 0.0011s you’re driving 10 cores at 100%? That would mean that clock() measurement is 10x that measured with tic and toc. Possible, unlikely.
    • clock(.) is grossly inaccurate, and consistent with the documentation, it is an approximate cpu time measurement! I suspect that it is pegged to the scheduler quantum size, but I didn’t dig through the Linux kernel code to check. I also didn’t check on other OSes, but this dude’s blog is consistent with that theory.

    So what to do… for starters, compare apples to apples! Next, make sure you take into account timer resolution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some code in JSP to download .docx files which is hosted
Let's assume I have a framework written in Java and some C++ code which
I have code written in c, which contains some complex algorithms and I want
I have written some code that loads an XML document using an XmlDocument object
I have written some code to look at properties using reflection. I have retrieved
I have some code written in Python which can not be transferred to a
I have some code written by someone else in which some functions take arguments
I have written a script which in turn runs some other code and I
I have written a some C code running on OS X 10.6, which happens
I have written some test scripts in python which call some apis for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.