Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Dictionary<&燃气轮机;_C#_Performance_Dictionary - Fatal编程技术网

C# 使用Dictionary<&燃气轮机;

C# 使用Dictionary<&燃气轮机;,c#,performance,dictionary,C#,Performance,Dictionary,字典性能似乎受到所存储项的大小的影响(这似乎很奇怪) 下面是我的简单课程: public class MyObject { public Guid Key { get; set; } } 还有两个简单的测试: private long _Iterations = 1000000; [TestMethod] public void ShouldTestDefaultConstructorPerformance() { for (var i = 0; i < _Iterat

字典性能似乎受到所存储项的大小的影响(这似乎很奇怪)

下面是我的简单课程:

public class MyObject
{
    public Guid Key { get; set; }
}
还有两个简单的测试:

private long _Iterations = 1000000;

[TestMethod]
public void ShouldTestDefaultConstructorPerformance()
{
    for (var i = 0; i < _Iterations; i++)
    {
        var obj = new MyObject() { Key = Guid.NewGuid() };
    }
}

[TestMethod]
public void ShouldTestDefaultGuidDictionaryPerformance()
{
    var dict = new Dictionary<Guid, MyObject>();
    for (var i = 0; i < _Iterations; i++)
    {
        var obj = new MyObject() { Key = Guid.NewGuid() };
        dict.Add(obj.Key, obj);
    }
}
现在,我将更改MyObject类:

public class MyObject
{
    public Guid Key { get; set; }

    private Dictionary<string, string> _Property0 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property1 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property2 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property3 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property4 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property5 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property6 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property7 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property8 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property9 = new Dictionary<string, string>();
}

在第二个测试中,对象构造需要花费1.72倍的时间,但是添加到字典需要花费6.11倍的时间。我预计测试需要更长的时间,但是为什么字典需要更长的时间来添加更大的对象呢?

添加到字典中的每个对象都会被赋予一个特殊的唯一标识符,以加速其在内存中的搜索和检索。这个特殊的唯一标识符(称为散列)是通过分析对象的整个内容来计算的。对象越大,计算散列的速度就越慢


如果您对其工作原理的详细信息感兴趣,请查看大学课程中的示例:

我认为
var obj=new MyObject(){Key=Guid.NewGuid()}
这一行实际上需要更长的时间,而不是字典的
Add()
。您是否使用了提供的内部测量方法?

我想我的答案是:使用轮廓仪,计算出哪一位实际需要更长的时间


这可能会突出显示实例化。也许:)

我认为人们需要更仔细地阅读问题,而不是匆忙发布答案。如果您仔细查看他的示例代码(两个测试),那么对于他的循环来说,具有Guid的MyObject和具有Guid和10个Dict的MyObject之间的区别在第二个(对象构造)下。但是,“添加字典”至少还需要5秒。

这将测试对象的创建以及插入字典的情况。您没有将一个与另一个隔离开来。这不是一个完全公平的比较,因为在第二个测试中,您正在分配
迭代*
,而在第一个测试中(取决于运行时引擎和优化),您正在重用相同的内存空间。更好的性能比较是测试1的对象数组,测试2的字典。。。我想你会发现性能更接近。我认为只有键被散列了(因为这就是dictionary用来计算内部使用哪个bucket的方法)?-1对象没有为添加到dictionary中提供唯一标识符。哈希值不是唯一的。对象“大小”不会直接影响哈希计算速度。做homework@Vijay在您的情况下,键对哈希值计算没有贡献。您必须重写GetHashCode才能正确运行。因为MyObject是一个不正确的类:@Brian编写绿色测试没问题,不是更多。不重写GetHashCode函数,而是停止基于哈希的集合的约定。由于性能存在问题,同样值得一提的是,基于散列的插入和查找的算法时间被打破了。通过SlimTune运行,这表明ctor花费的时间最长。今天的教训:不要总是相信VS IDE中显示的持续时间数字!非常感谢。下次我做同样的事情时,我会回来提醒自己:)
public class MyObject
{
    public Guid Key { get; set; }

    private Dictionary<string, string> _Property0 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property1 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property2 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property3 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property4 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property5 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property6 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property7 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property8 = new Dictionary<string, string>();
    private Dictionary<string, string> _Property9 = new Dictionary<string, string>();
}
ShouldTestDefaultConstructorPerformance    : 00:00:01.333
ShouldTestDefaultGuidDictionaryPerformance : 00:00:07.556