[英]How can I add a precision within 2 seconds c#
I'm just wondering how I can add a precision of 2 seconds. 我只是想知道如何增加2秒的精度。 At the moment my code works how i'd like but I'd like to add a precision of 2000 miliseseconds.
目前,我的代码可以按我希望的方式工作,但是我想增加2000毫秒的精度。
[TestMethod]
public void LocationNameSearch()
{
// Create new stopwatch.
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
// Import the search method.
System.Threading.Thread.Sleep(2000);
// Stop timing.
stopwatch.Stop();
//Assert
Assert.IsTrue(stopwatch.ElapsedMilliseconds < 3000, **2000**);
}
If you mean that you want to have a range of 2000 milliseconds over and under a specific value then you could use the following which checks if the elapsed milliseconds are within 2000 milliseconds of 3000. 如果您想在特定值的上方和下方指定2000毫秒的范围,则可以使用以下代码检查经过的毫秒数是否在3000的2000毫秒内。
Assert.IsTrue(stopwatch.ElapsedMilliseconds <= 3000 + 2000
&& stopwatch.ElapsedMilliseconds >= 3000 - 2000
);
Edit: According to the comments it sounds like more of a differentation is needed: 编辑:根据评论,听起来需要更多区别:
if (stopwatch.ElapsedMilliseconds >= 3000)
{
if (stopwatch.ElapsedMilliseconds > 5000)
{
Assert.Fail("Error");
}
else
{
// Generate warning
Assert.Fail("Warning");
}
}
If you mean the least scale is 2000
then you should do this. 如果您的最小标度是
2000
则应该这样做。
var lowprec = (stopwatch.ElapsedMilliseconds/2000)*2000; // notice the integer division
if(lowprec < 3000)
{
//pass
}
else if(lowprec < 5000)
{
//warn
}
Here assume time elapsed is 3542
milliseconds. 此处假设经过的时间为
3542
毫秒。 lowprec
becomes 2000
(since its highest precision) and it will pass. lowprec
变为2000
(因为它的最高精度),并且它将通过。
If time elapsed is 4032
milliseconds it lowprec
becomes 4000
and then its warning. 如果经过的时间是
4032
毫秒,则lowprec
变为4000
,然后发出警告。
But If i were you i would just compare results normally. 但是,如果我是你,我只会正常比较结果。 in programming we dont have 50% true 50% false.
在编程中,我们没有50%正确50%错误。 it does not make sense.
它没有任何意义。 we only have 100% true or 100% false.
我们只有100%正确或100%错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.