Sub-millisecond precision timing in C or C++

2019-01-12 00:00发布

What techniques / methods exist for getting sub-millisecond precision timing data in C or C++, and what precision and accuracy do they provide? I'm looking for methods that don't require additional hardware. The application involves waiting for approximately 50 microseconds +/- 1 microsecond while some external hardware collects data.

EDIT: OS is Wndows, probably with VS2010. If I can get drivers and SDK's for the hardware on Linux, I can go there using the latest GCC.

标签: c++ c time
9条回答
孤傲高冷的网名
2楼-- · 2019-01-12 00:39

You could try the technique described here, but it's not portable.

查看更多
爷的心禁止访问
3楼-- · 2019-01-12 00:40

Most modern processors have registers for timing or other instrumentation purposes. On x86 since Pentium days there is the RDTSC instruction, for example. You compiler may give you access to this instruction.

See wikipedia for more info.

查看更多
Deceive 欺骗
4楼-- · 2019-01-12 00:40

Good luck trying to do that with MS Windows. You need a realtime operating system, that is to say, one where timing is guaranteed repeatable. Windows can switch to another thread or even another process at an inopportune moment. You will also have no control over cache misses.

When I was doing realtime robotic control, I used a very lightweight OS called OnTime RTOS32, which has a partial Windows API emulation layer. I do not know if it would be suitable for what you are doing. However, with Windows, you will probably never be able to prove that it will never fail to give the timely response.

查看更多
贼婆χ
5楼-- · 2019-01-12 00:41

boost::datetime has microsecond precision clock but its accuracy depends on the platform.

The documentation states:

ptime microsec_clock::local_time() "Get the local time using a sub second resolution clock. On Unix systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution."

http://www.boost.org/doc/libs/1_43_0/doc/html/date_time/posix_time.html#date_time.posix_time.ptime_class

查看更多
够拽才男人
6楼-- · 2019-01-12 00:42

You may try the following:

struct timeval t; gettimeofday(&t,0x0);

This gives you current timestamp in micro-seconds. I am not sure about the accuracy.

查看更多
疯言疯语
7楼-- · 2019-01-12 00:42

timeval in sys/time.h has a member 'tv_usec' which is microseconds.

This link and the code below will help illustrate:

http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/time.h.html

timeval start;
timeval finish;

long int sec_diff;
long int mic_diff;

gettimeofday(&start, 0);
cout << "whooo hooo" << endl;
gettimeofday(&finish, 0);

sec_diff = finish.tv_sec - start.tv_sec;
mic_diff = finish.tv_usec - start.tv_usec;

cout << "cout-ing 'whooo hooo' took " << sec_diff << "seconds and " << mic_diff << " micros." << endl;

gettimeofday(&start, 0);
printf("whooo hooo\n");
gettimeofday(&finish, 0);

sec_diff = finish.tv_sec - start.tv_sec;
mic_diff = finish.tv_usec - start.tv_usec;

cout << "fprint-ing 'whooo hooo' took " << sec_diff << "seconds and " << mic_diff << " micros." << endl;
查看更多
登录 后发表回答