.Net 中的计时器和循环是否准确? [英] Are Timers and Loops in .Net accurate?

查看:18
本文介绍了.Net 中的计时器和循环是否准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发一个程序来计算 555 定时器 IC 产生的脉冲的频率和脉冲宽度时,通过 PC 并行端口进入 PC.我注意到每次运行代码时它都会显示不同的值,所以我开始测试循环和计时器的准确性.我已经运行了以下代码并发现它们不准确(我可能错了,如果我错了,请纠正我!):

While developing a program to calculate the frequency and pulse width of the pulse generated by a 555 timer IC, coming to the PC via PC Parallel port. I noticed that every time I run the code it shows different values, so I start testing the Loops and timers for there accuracy. I have run the following code and come to the point that they are inaccurate (I might be wrong, please correct me, if I am!):

对于计时器:

    int sec = 0;
    private void button2_Click(object sender, EventArgs e)
    {
        sec = DateTime.Now.Second;
        i = 0;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (sec == DateTime.Now.Second)
        {
            i++;
        }
        else
        {
            timer1.Enabled = false;
            MessageBox.Show(i.ToString(),"Timer Output");
        }
    }

输出:应该是一样的,但是:

对于循环:

    private void button1_Click(object sender, EventArgs e)
    {
        i = 0;
        CheckForIllegalCrossThreadCalls = false;
        Thread t1 = new Thread(LoopTest);
        t1.Start();
    }

    void LoopTest()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        this.Cursor = Cursors.WaitCursor;
        while (true)
        {
            if (sw.ElapsedMilliseconds != 1000)
            {
                i++;
            }
            else
            {
                break;
            }
        }
        sw.Stop();
        this.Cursor = Cursors.Default;
        MessageBox.Show(i.ToString(), "Loop Output");
    }

输出:应该是一样的,但是:

我应该怎么做才能使循环和计时器准确,有什么办法吗?或者我必须去硬而复杂的C代码和DOS?

What should I do to make loops and timer accurate, Is there any way to do it? Or I have to go to hard and complex C code and DOS?

我认为这是在这个问题中给我错误值的基本原因:计算并口输入频率 - C#

I think it is the basic reason for getting me wrong values in this question: Count Parallel port input frequency - C#

推荐答案

1) 不要使用 DateTime.Now 进行性能测量,使用 StopWatch.

1) Don't use DateTime.Now for performance measurements, use StopWatch.

2) "OUTPUT: 应该是一样的,但是.."

他们为什么要这样做?您正在非 RTOS(实时操作系统)上运行托管/JIT 代码.如果操作系统愿意,您的代码可以随时退回.是什么让您相信在这种环境中运行 N 次相同的代码应该总是在如此小的程度上产生相同的结果?

Why should they? You are running managed/JIT'ed code on a non RTOS (real time operating system). You're code can get bounced at any time if the OS feels like it. What lead you to believe that running the same code N times in this environment should always produce the same results to such a small degree?

3) Windows 上的计时器的分辨率约为 15 毫秒.获得非常准确计时的最佳选择是支持它的系统(CPU)上的 HighPerformanceTimer API.你甚至没有向我们展示计时器的时间间隔.

3) Timers on Windows have a ~15ms resolution. Your best bet for very accurate timings is the HighPerformanceTimer API on systems (CPU's) which support it. You haven't even shown us the timer's interval.

您没有在这里考虑许多变量,并且您的预测基于错误的假设.你甚至测量这个代码多少次?您是否考虑了第一次编译所需的时间?您是否在发布模式下运行?通过VS?是否有很多任务在后台运行?我可以继续.

You're failing to consider many variables here and you're basing your predictions upon faulty assumptions. How many times are you even measuring this code? Are you taking into account the time needed to compile it the first time through? Are you running in release mode? Through VS? Are there many tasks running in the background? I could go on.

这篇关于.Net 中的计时器和循环是否准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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