MATLAB Answers

0

cputime not working as expected

Asked by Jim Hokanson on 27 Nov 2016
Latest activity Commented on by Steven Lord
on 27 Nov 2016
It seems like cputime does not work as advertised on Mac OSX 10.11.6 and Matlab 2016b or Windows 7 Matlab 2016a. cputime should be in seconds, but running this command, then waiting for many seconds, and then running it again. Here's some example code, comparing it to tic/toc. The numbers aren't even close ...
t1 = tic; t2 = cputime; pause(5); t3 = cputime-t2; t4 = toc(t1); disp(t3); disp(t4);
I don't use cputime() normally, but I was exploring another issue with a mismatch between tic/toc and some internally timed c code (will post as another issue) and I wanted to see how cputime compared.

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Jim Hokanson on 27 Nov 2016
 Accepted Answer

cputime measures the # of clock cycles (summed across all threads) that the Matlab process has used since Matlab started. Units are in seconds. cputime does not measure elapsed "wall time" like tic/toc does. Most often "elapsed" cputime (i.e. the difference between two cputime()) calls is less than wall time, because processors aren't using every clock cycle to process Matlab commands. However, since this is summed across threads, it is possible to have elapsed cputime be larger than wall time, in cases in which multiple threads are executing Matlab commands at the same time.

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 27 Nov 2016

From this documentation page: "The CPU time for the pause function is typically small, but the wall-clock time accounts for the actual time that MATLAB execution is paused. Therefore, the wall-clock time might be longer."
If you went to the gym and worked out for an hour, you'd be more tired than if you took an hour long nap. By the wall clock (as measured by the tic and toc functions) you spent the same amount of time in both activities, but by the activity meter (the cputime function) you did a lot more work at the gym than you did sleeping.

  2 Comments

-- face-palm --
yikes, 'rookie' mistake
I was having an issue with some C code, which at the end of the day came down to the same issue with different behavior based on operating system (wall clock vs processor time) and for some reason cpu_time was really throwing me for a loop. For what it is worth, the description in the function documentation is pretty worthless.
Steven Lord
on 27 Nov 2016
If you have suggestions for information you were hoping to find but didn't in the function documentation, please click the "No" button on the "Did this help?" question near the bottom of the documentation page and describe how you would like to see the documentation staff improve the page.
I know for a fact the documentation staff reads this feedback. [Some of the documentation staff members with whom I work have asked me to review changes they want to make in response to feedback submitted this way.]

Sign in to comment.