1

我可以使用什么操作来模拟一个相对昂贵的过程

public void SimulateLongProcess()
{
   //Do Something that takes a few seconds to perform on a quadcode CPU
}

我想避免任何线程等待左右,也不涉及 IO。一些数学运算可以做...

4

2 回答 2

2

不确定你到底需要什么。您可以执行以下操作,在我的机器上大约需要 2900 毫秒。这段代码不能保证准确的执行时间,不同机器的执行时间会有所不同。您可能需要根据需要对其进行调整。

        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

        for (int i = 0; i < 10000000; i++)
        {
            double test = 1000; 
            test = (test * 100000.00001)/100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;
            test = (test * 100000.00001) / 100000.123123;

        }

        stopWatch.Stop();
        var executionTime = stopWatch.ElapsedMilliseconds;
于 2012-06-26T11:11:14.643 回答
0

作为一个非常愚蠢的事情,你可以只运行一个 for 循环

for (int i = 0; i < 1000000000; i++)
{
    var str = "dummy";
} 

Thread.Sleep会更精确。确保仅将其用于测试

于 2012-06-26T11:01:08.393 回答