加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

struct array vs object array c#

发布时间:2020-12-15 23:47:46 所属栏目:百科 来源:网络整理
导读:我知道可变结构是邪恶的.但是,我仍然想比较一个结构数组与一个对象数组的性能.这就是我到目前为止所拥有的 public struct HelloStruct { public int[] hello1; public int[] hello2; public int hello3; public int hello4; public byte[] hello5; public by
我知道可变结构是邪恶的.但是,我仍然想比较一个结构数组与一个对象数组的性能.这就是我到目前为止所拥有的

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,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();

    }

这里发生了一些我想要了解的事情.

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

其次,当我比较CompareTimesClassVsStruct()中的时间时,我得到大致相同的时间.这背后的原因是什么?是否有任何情况下使用结构数组或对象数组会胜过另一个?

谢谢

解决方法

当您访问结构数组元素的属性时,您不是在结构的副本上操作 – 您正在对结构本身进行操作. (这不适用于List< SomeStruct>您将在副本上操作,并且您的示例中的代码甚至不会编译.)

你看到相似时间的原因是因为时间被(j%100).ToString()和new int [] {1,j%50}扭曲了;循环内.这两个语句所花费的时间使数组元素访问所花费的时间相形见绌.

我稍微更改了测试应用程序,并且我有时间访问9.3s的struct数组和10s的类数组(对于1,000,000个循环),因此struct数组明显更快,但非常微不足道.

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

类数组的元素不相邻(当然,虽然引用了数组中的元素),但是当您遍历数组时,这会导致更多的处理器缓存未命中.

另一件需要注意的事情是,struct数组中连续字节的数量是有效的(元素数)*(sizeof(element)),而类数组中的连续字节数是(元素数)*( sizeof(reference))其中引用的大小是32位或64位,具体取决于内存模型.

这可能是大型结构数组的问题,其中数组的总大小将超过2 ^ 31个字节.

您可能在速度上看到的另一个区别是将大型结构作为参数传递 – 显然,将值的引用副本传递给堆栈上的引用类型比通过值传递大型结构的副本要快得多.

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

根据经验,结构的大小不应超过32个字节(确切的限制是争论的问题),它们应该只包含原始(blittable)类型,它们应该是不可变的.而且,通常情况下,你不应该担心结构的结构,除非你有一个可证明的性能需求.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读