c# stopwatch.startnew() 总是 returns 0
c# stopwatch.startnew() always returns 0
我有这个循环,我想测量它的速度:
private static string[] sequentialTest(string[] thisstringlist)
{
var watch = Stopwatch.StartNew();
string[] returnList = new string[2150];
for (int i = 0; i < thisstringlist.Length; ++i)
{
int thisI = i;
returnList[i] = thisstringlist[i] + "pluuus this";
}
int thiss = 0;
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("execution time" + watch.ElapsedMilliseconds);
return returnList;
}
应该很简单,秒表应该只是测量这个操作需要多长时间,但我一直返回 0
我也试过插入thread.sleep(),只是为了强制代码花费一些时间,但这也不起作用
顾名思义,秒表需要 stopped 才能看到所有这些值,如文档中所示。所以为了解决这个问题,在 var elapsedMs = watch.ElapsedMilliseconds;
.
之前写 watch.Stop();
注意:在较新版本的 .NET 框架中,不需要 stop
。
此外,检查 docs
private static string[] sequentialTest(string[] thisstringlist)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string[] returnList = new string[2150];
for (int i = 0; i < thisstringlist.Length; ++i)
{
int thisI = i;
returnList[i] = thisstringlist[i] + "pluuus this";
}
int thiss = 0;
Console.WriteLine("execution time" + stopwatch.Elapsed.TotalMilliseconds);
stopwatch.Stop();
}
我有这个循环,我想测量它的速度:
private static string[] sequentialTest(string[] thisstringlist)
{
var watch = Stopwatch.StartNew();
string[] returnList = new string[2150];
for (int i = 0; i < thisstringlist.Length; ++i)
{
int thisI = i;
returnList[i] = thisstringlist[i] + "pluuus this";
}
int thiss = 0;
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("execution time" + watch.ElapsedMilliseconds);
return returnList;
}
应该很简单,秒表应该只是测量这个操作需要多长时间,但我一直返回 0
我也试过插入thread.sleep(),只是为了强制代码花费一些时间,但这也不起作用
顾名思义,秒表需要 stopped 才能看到所有这些值,如文档中所示。所以为了解决这个问题,在 var elapsedMs = watch.ElapsedMilliseconds;
.
watch.Stop();
注意:在较新版本的 .NET 框架中,不需要 stop
。
此外,检查 docs
private static string[] sequentialTest(string[] thisstringlist)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string[] returnList = new string[2150];
for (int i = 0; i < thisstringlist.Length; ++i)
{
int thisI = i;
returnList[i] = thisstringlist[i] + "pluuus this";
}
int thiss = 0;
Console.WriteLine("execution time" + stopwatch.Elapsed.TotalMilliseconds);
stopwatch.Stop();
}