我可以使用什么操作来模拟相对昂贵的过程
public void SimulateLongProcess()
{
//Do Something that takes a few seconds to perform on a quadcode CPU
}
我想避免任何线程等待,也不要涉及IO。一些数学运算可以做......
答案 0 :(得分: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;
答案 1 :(得分:0)
作为一个非常虚拟的东西,你可以运行一个for循环
for (int i = 0; i < 1000000000; i++)
{
var str = "dummy";
}
Thread.Sleep
会更准确。确保仅将其用于测试