Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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#_C#_Arrays_Struct - Fatal编程技术网

结构数组与对象数组c#

结构数组与对象数组c#,c#,arrays,struct,C#,Arrays,Struct,我知道可变结构是邪恶的。但是,我仍然想比较结构数组和对象数组的性能。这就是我目前所拥有的 public struct HelloStruct { public int[] hello1; public int[] hello2; public int hello3; public int hello4; public byte[] hello5; public byte[] hello6;

我知道可变结构是邪恶的。但是,我仍然想比较结构数组和对象数组的性能。这就是我目前所拥有的

 public struct HelloStruct
    {
        public int[] hello1;
        public int[] hello2;
        public int hello3;
        public int hello4;
        public byte[] hello5;
        public byte[] hello6;
        public string hello7;
        public string hello8;
        public string hello9;
        public SomeOtherStruct[] hello10;

    }

    public struct SomeOtherStruct
    {
        public int yoyo;
        public int yiggityyo;
    }

    public class HelloClass
    {
        public int[] hello1;
        public int[] hello2;
        public int hello3;
        public int hello4;
        public byte[] hello5;
        public byte[] hello6;
        public string hello7;
        public string hello8;
        public string hello9;
        public SomeOtherClass[] hello10;

    }
        public class SomeOtherClass
    {
        public int yoyo;
        public int yiggityyo;
    }

 static void compareTimesClassVsStruct()
    {
        HelloStruct[] a = new HelloStruct[50];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = default(HelloStruct);
        }

        HelloClass[] b = new HelloClass[50];
        for (int i = 0; i < b.Length; i++)
        {
            b[i] = new HelloClass();
        }
        Console.WriteLine("Starting now");
        var s1 = Stopwatch.StartNew();
        for (int i = 0; i < _max; i++)
        {
            a[i % 50].hello1 = new int[] { 1, 2, 3, 4, i % 50 };
            a[i % 50].hello3 = i;
            a[i % 50].hello7 = (i % 100).ToString();
        }
        s1.Stop();

        var s2 = Stopwatch.StartNew();
        for (int j = 0; j < _max; j++)
        {
            b[j % 50].hello1 = new int[] { 1, 2, 3, 4, j % 50 };
            b[j % 50].hello3 = j;
            b[j % 50].hello7 = (j % 100).ToString();
        }
        s2.Stop();

        Console.WriteLine(((double)(s1.Elapsed.TotalSeconds)));
        Console.WriteLine(((double)(s2.Elapsed.TotalSeconds)));
        Console.Read();

    }
public结构HelloStruct
{
公共int[]hello1;
公共int[]hello2;
公共int hello3;
公共int hello4;
公共字节[]hello5;
公共字节[]hello6;
公共字符串hello7;
公共字符串hello8;
公共字符串hello9;
public SomeOtherStruct[]hello10;
}
公共结构SomeOtherStruct
{
公共国际yoyo;
yiggityyo公共图书馆;
}
公共类HelloClass
{
公共int[]hello1;
公共int[]hello2;
公共int hello3;
公共int hello4;
公共字节[]hello5;
公共字节[]hello6;
公共字符串hello7;
公共字符串hello8;
公共字符串hello9;
公共其他类[]他10;
}
公共类其他类
{
公共国际yoyo;
yiggityyo公共图书馆;
}
静态void compareTimesClassVsStruct()
{
HelloStruct[]a=新的HelloStruct[50];
for(int i=0;i
我想了解这里发生的一些事情

首先,由于数组存储结构,当我尝试使用索引操作从数组访问结构时,我应该获取结构的副本还是原始结构的引用?在这种情况下,当我在运行代码后检查数组时,我会得到经过修改的struct值。为什么会这样

其次,当我比较
CompareTimesClassVsStruct()
中的计时时,得到的时间大致相同。这背后的原因是什么?在任何情况下,使用结构数组或对象数组的性能是否优于其他数组


谢谢

当您访问结构数组元素的属性时,您不是在操作结构的副本,而是在操作结构本身。(在
列表中,您将对副本进行操作,而您的示例中的代码甚至无法编译,这一点不适用。)

您看到类似时间的原因是时间被
(j%100)扭曲了在循环中。这两条语句所花费的时间与数组元素访问所花费的时间相形见绌

我对测试应用程序做了一些修改,获得了访问9.3s的结构数组和10s的类数组(100000000个循环)的时间,因此结构数组的速度明显更快,但这并不重要

可以使结构数组更快地迭代的一件事是引用的局部性。在结构数组上迭代时,相邻元素在内存中是相邻的,这减少了处理器缓存未命中的数量

类数组的元素不是相邻的(尽管对数组中元素的引用当然是相邻的),这可能会在迭代数组时导致更多的处理器缓存未命中

另一件需要注意的事情是,结构数组中的连续字节数实际上是
(元素数)*(sizeof(元素))
,而类数组中的连续字节数是
(元素数)*(sizeof(引用))
,其中引用的大小是32位或64位,取决于内存型号

对于大型结构的大型数组,如果数组的总大小超过2^31字节,则可能会出现问题

在速度方面,您可能会看到的另一个差异是将大型结构作为参数传递时——显然,按值传递引用的副本到堆栈上的引用类型要比按值传递大型结构的副本快得多

最后,请注意,示例结构不是很有代表性。它包含许多引用类型,所有引用类型都存储在堆的某个位置,而不是数组本身

根据经验,结构的大小不应该超过32字节左右(确切的限制有待商榷),它们应该只包含基本(blittable)类型,并且应该是不可变的。而且,通常情况下,您不应该担心如何使事物结构化,除非您对它们有可证明的性能需求

首先,由于数组存储结构,当我尝试使用索引操作从数组访问结构时,我应该获取结构的副本还是原始结构的引用

让我告诉你实际发生了什么,而不是回答你措词混乱的问题

  • 数组是变量的集合
  • 应用于数组时的索引操作将生成一个变量
  • 成功变异可变结构的字段需要手头有包含要变异的结构的变量
现在来回答你的问题:你应该