我可以在Linux上获得最佳的时间分辨率 [英] What's the best timing resolution can i get on Linux

查看:144
本文介绍了我可以在Linux上获得最佳的时间分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测量并行端口上2个信号之间的时间差,但首先我知道我的测量系统(AMD Athlon(tm)64 X2双核处理器5200+×2)有多少精度. )在SUSE 12.1 x64上.

I'm trying to measure the time difference between 2 signals on the parallel port, but first i got to know how much accurate and precise is my measuring system (AMD Athlon(tm) 64 X2 Dual Core Processor 5200+ × 2) on SUSE 12.1 x64.

因此,经过一番阅读后,我决定使用clock_gettime(),首先,我使用以下代码获取clock_getres()的值:

So after some reading i decide to use clock_gettime(), first i get the clock_getres() value using this code:

/*
 * This program prints out the clock resolution.
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( void )
  {
    struct timespec res;

    if ( clock_getres( CLOCK_REALTIME, &res) == -1 ) {
      perror( "clock get resolution" );
      return EXIT_FAILURE;
    }
    printf( "Resolution is %ld nano seconds.\n",
          res.tv_nsec);
    return EXIT_SUCCESS;
  }

,结果是:1纳秒.我真开心!

and the out was: 1 nano second. And i was so happy!!

但这是我的问题,当我尝试使用其他代码检查该事实时:

But here is my problem, when i tried to check that fact with this other code:

#include <iostream>
#include <time.h>
using namespace std;

timespec diff(timespec start, timespec end);

int main()
{
    timespec time1, time2, time3,time4;
    int temp;
    time3.tv_sec=0;
    time4.tv_nsec=000000001L;
    clock_gettime(CLOCK_REALTIME, &time1);
        NULL;
    clock_gettime(CLOCK_REALTIME, &time2);
    cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
    return 0;
}

timespec diff(timespec start, timespec end)
{
    timespec temp;
    if ((end.tv_nsec-start.tv_nsec)<0) {
        temp.tv_sec = end.tv_sec-start.tv_sec-1;
        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }
    return temp;
}

此函数计算两次调用clock_gettime之间的时间,声明了time3和time4,但在此示例中未使用,因为我正在使用它们进行测试.

this one calculate the time between the two calls of clock_gettime, the time3 and time4 are declared but not used in this example because i was doing tests with them.

在此示例中,输出在978到1467 ns之间波动.这两个数字都是489的倍数,这使我认为489 ns是我的真实分辨率.与上面获得的1 ns相距很远.

The output in this example is fluctuating between 978 and 1467 ns. both numbers are multiples of 489, this make me think that 489 ns is my REAL resolution. far far from the 1 ns obtained above.

我的问题:有什么办法可以得到更好的结果?我想念什么吗?

My question: is there ANY WAY of getting better results? am i missing something?

我的项目确实至少需要10ns的分辨率.快点! GPS可以获得比PC更好的分辨率?

I really need at least 10ns resolution for my project. Come on! a GPS can get better resolution than a PC??

推荐答案

据我所知,Linux 在PC上运行通常无法为您提供计时器精度(以纳秒为单位).这主要是由于内核中使用的任务/流程调度程序的类型.这既是内核的结果,也是硬件的结果.

As far as I know, Linux running on a PC will generally not be able to give you timer accuracy in the nanoseconds range. This is mainly due to the type of task/process scheduler used in the kernel. This is as much a result of the kernel as it is of the hardware.

如果您需要具有纳秒级分辨率的计时,恐怕您不走运.但是,您应该能够获得微秒的分辨率,对于大多数情况-包括并行端口应用程序,该分辨率都应该足够好.

If you need timing with nanosecond resolution I'm afraid that you're out of luck. However you should be able to get micro-second resolution which should be good enough for most scenarios - including your parallel port application.

如果您需要在纳秒级范围内计时以精确到纳秒级,则很可能需要专用的硬件解决方案;使用真正准确的振荡器(作为比较,大多数x86 CPU的基本时钟频率在乘数之前都在兆赫兹的范围内)

If you need timing in the nano-seconds range to be accurate to the nano-second you will need a dedicated hardware solution most likely; with a really accurate oscillator (for comparison, the base clock frequency of most x86 CPUs is in the range of mega-hertz before the multipliers)

最后,如果您要用计算机代替示波器的功能,那么示波器将无法在相对较低的频率范围内工作.您最好投资一个示波器,甚至简单,可移植,可以插入计算机以显示数据.

Finally, if you're looking to replace the functionality of an oscilloscope with your computer that's just not going to work beyond relatively low frequency signals. You'd be much better off investing in a scope - even a simple, portable, hand-held that plugs into your computer for displaying the data.

这篇关于我可以在Linux上获得最佳的时间分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆