C# 为什么我的ElapsedMills在这里总是零?

C# 为什么我的ElapsedMills在这里总是零?,c#,.net,performance,data-structures,C#,.net,Performance,Data Structures,因此,我试图衡量我创建的哈希集的性能与列表和下面代码块中相同元素的性能 Stopwatch Watch = new Stopwatch(); long tList = 0, tHset = 0; // ms foreach ( string Str in Copy ) { // measure time to look up string in ordinary list Watch.

因此,我试图衡量我创建的哈希集的性能与
列表和下面代码块中相同元素的性能

        Stopwatch Watch = new Stopwatch();
        long tList = 0, tHset = 0; // ms
        foreach ( string Str in Copy )
        {
            // measure time to look up string in ordinary list
            Watch.Start();
            if ( ListVersion.Contains(Str) ) { }
            Watch.Stop();
            tList += Watch.ElapsedMilliseconds;
            // now measure time to look up same string in my hash set
            Watch.Reset();
            Watch.Start();
            if ( this.Contains(Str) ) { }
            Watch.Stop();
            tHset += Watch.ElapsedMilliseconds;
            Watch.Reset();
        }
        int n = Copy.Count;
        Console.WriteLine("Average milliseconds to look up in List: {0}", tList / n);
        Console.WriteLine("Average milliseconds to look up in hashset: {0}", tHset / n);

它同时输出
0
。知道为什么吗?相关文档:

这是因为操作速度快于
Stapwatch
的精度

不要测量每个
包含的
调用,而是单独测量一组:

Stopwatch Watch = new Stopwatch();
long tList = 0, tHset = 0; // ms

// measure time to look up string in ordinary list
Watch.Start();
foreach ( string Str in Copy )
{
    if ( ListVersion.Contains(Str) ) { }
}
Watch.Stop();
tList = Watch.ElapsedMilliseconds;
// now measure time to look up same string in my hash set
Watch.Reset();
Watch.Start();
foreach ( string Str in Copy )
{
    if ( this.Contains(Str) ) { }
}
Watch.Stop();
tHset = Watch.ElapsedMilliseconds;

Console.WriteLine("Total milliseconds to look up in List: {0}", tList);
Console.WriteLine("Total milliseconds to look up in hashset: {0}", tHset);
如您所见,我还将代码更改为打印所花费的总时间,而不是平均时间。对于如此快速的操作,性能通常以Xs/Y操作表示,而不是以平均值表示。例如,每1000万次查找需要40毫秒


而且,在发布模式下,部分代码可能会被优化掉,因为它实际上什么都不做。考虑< <代码>包含的元素的计数数量返回true,并在末尾打印该数字。

< P>这是因为操作比 StAppWist> <代码>的精度要快。 不要测量每个
包含的
调用,而是单独测量一组:

Stopwatch Watch = new Stopwatch();
long tList = 0, tHset = 0; // ms

// measure time to look up string in ordinary list
Watch.Start();
foreach ( string Str in Copy )
{
    if ( ListVersion.Contains(Str) ) { }
}
Watch.Stop();
tList = Watch.ElapsedMilliseconds;
// now measure time to look up same string in my hash set
Watch.Reset();
Watch.Start();
foreach ( string Str in Copy )
{
    if ( this.Contains(Str) ) { }
}
Watch.Stop();
tHset = Watch.ElapsedMilliseconds;

Console.WriteLine("Total milliseconds to look up in List: {0}", tList);
Console.WriteLine("Total milliseconds to look up in hashset: {0}", tHset);
如您所见,我还将代码更改为打印所花费的总时间,而不是平均时间。对于如此快速的操作,性能通常以Xs/Y操作表示,而不是以平均值表示。例如,每1000万次查找需要40毫秒


而且,在发布模式下,部分代码可能会被优化掉,因为它实际上什么都不做。考虑< <代码>包含的元素的计数数目返回true,并在末尾打印该数字。

您可以保持代码,而不是这样做:

Watch.ElapsedMilliseconds
您可以这样做:

Watch.Elapsed.TotalMilliseconds

这样,您将获得毫秒的小数部分,您可以保持代码的原样,而不是执行以下操作:

Watch.ElapsedMilliseconds
您可以这样做:

Watch.Elapsed.TotalMilliseconds

这样你就得到了毫秒的小数部分,因为运算太快了,你不应该这样测量性能。因为运算太快了,你不应该这样测量性能。嗯?如果我的编译器假设我的
if
语句在program@user6048670但是你的if什么都不做。如果编译器认识到这一点,它怎么会愚蠢呢?它必须是一个非常聪明的编译器,才能理解对容器的
Contains
方法的调用没有副作用。我怀疑汇编元数据是否包含足够的信息来告诉编译器类似的事情。嗯?如果我的编译器假设我的
if
语句在program@user6048670但是你的if什么都不做。如果编译器认识到这一点,它怎么会愚蠢呢?它必须是一个非常聪明的编译器,才能理解对容器的
Contains
方法的调用没有副作用。我怀疑汇编元数据是否包含足够的信息来告诉编译器类似的事情。