ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Which one is more precise: ros::WallTime::now() OR clock_gettime? [closed]

asked 2021-05-01 22:38:00 -0600

Eman.m gravatar image


I want to measure the time it takes from navfn to generate a path:

begin_ = ros::WallTime::now();
end_ = ros::WallTime::now();

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time3);

double run_time = (time1.tv_nsec)*1e-6; // milliseconds
double execution_time = (end_ - begin_).toNSec() * 1e-6;

ROS_INFO_STREAM("Exectution time (ms) using ROS WallTime: " << execution_time);
ROS_INFO_STREAM("Run time (ms) using C++ clock_gettime: " << run_time);

Both given results in ms, but which one is more precise?

This is the output:

image description

edit retag flag offensive reopen merge delete

Closed for the following reason Question does not follow our guidelines for questions. Please see: for more details. by gvdhoorn
close date 2021-05-02 02:30:59.159149


I'm closing your question, as it does not follow the support guidelines, specifically: do not post screenshots of terminals.

There would be no need, as it's all text.

Simply copy-paste it into your question and make sure to format it correctly (use the Preformatted Text button, the one with 101010 on it).

You do not need to post a new question, just edit your current one. Use the edit button/link to do that.

After you've replaced the screenshot with the actual text, we can re-open your question.

gvdhoorn gravatar image gvdhoorn  ( 2021-05-02 02:32:38 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-05-02 02:29:05 -0600

gvdhoorn gravatar image

I don't believe this has something to do with accuracy.

In one case, you execute:


In the other:

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...);

Wall time != CPU time.

The former essentially returns the current value of the regular clock (ie: the same as the one hanging on your wall, or the one displayed by your watch).

The latter returns the time as "experienced" by your CPU for a particular process.

Wikipedia has a rather nice description (on the CPU time page):

CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to elapsed time, which includes for example, waiting for input/output (I/O) operations or entering low-power (idle) mode. The CPU time is measured in clock ticks or seconds.

So you cannot really compare the two.

I want to measure the time it takes from navfn to generate a path

In case of benchmarking specific bits of code, and you know what you're measuring is most likely CPU bound, then you should use CPU time. Using wall time for benchmarking is only useful in very specific cases.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2021-05-01 22:38:00 -0600

Seen: 458 times

Last updated: May 02 '21