包括定时器和循环的净准确吗? [英] Are Timers and Loops in .Net accurate?

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

问题描述

在开发一个程序来计算由一个555计时器产生的脉冲的频率和脉冲宽度,来通过PC并行口的个人电脑。我注意到,每次我运行code也表现出不同的价值观,所以我开始测试循环和定时器有准确性。我已经运行下面的code和前来,他们​​是不准确的地步(我可能是错的,请纠正我,如果我在这里!):

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");
        }
    }

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

OUTPUT: Should be same, but:

for循环:

    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");
    }

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

OUTPUT: Should be same, but:

我该怎么做才能让循环和计时准确,有没有办法做到这一点?或者我必须去努力,复杂的C code和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 的性能测试,使用秒表

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

2),输出:应该是一样的,但..

他们为什么要?在运行管理/ JIT'ed code在非RTOS(实时操作系统)。你是code可以随时得到反弹,如果操作系统感觉它。是什么让你相信,在这样的环境中运行相同的code 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定时器有〜15ms的分辨率。你最好的非常精确计时的办法是在系统(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.

你不能在这里考虑许多变量,并在错误的假设你的基础predictions。多少次,你甚至测量该code?你是否考虑到编译它第一次通过所需的时间?你是否在释放模式运行?通过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.

这篇关于包括定时器和循环的净准确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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