简体   繁体   English

C# 访问 3D 结构数组的最佳方式

[英]C# best way to access 3D array of structs

Hello fellow programmers.各位程序员朋友们好。 So I have a structure, and have come up with 3 different 3D arrays to store it in. Thing is, optimization is not my specialty and I need advice from someone who knows their stuff regarding best ways to store and access multi dimensional arrays of structures.所以我有一个结构,并提出了 3 个不同的 3D 数组来存储它。 问题是,优化不是我的专长,我需要了解他们关于存储和访问多维结构数组的最佳方法的人的建议. I need to know if any of these methods are better in terms of memory use, or speed of access, etc. when compared to each other.我需要知道这些方法中的任何一种在内存使用或访问速度等方面是否比彼此更好。 Is there an even better way to store and access the structure aside from arrays that I am not aware of?除了我不知道的数组之外,还有更好的方法来存储和访问结构吗? I mainly used https://msdn.microsoft.com/en-us/library/2s05feca.aspx and other stack overflow posts for reference.我主要使用了https://msdn.microsoft.com/en-us/library/2s05feca.aspx等堆栈溢出帖子作为参考。

I'm aware that performance and memory use is probably negligible when it comes to small data sets, but the reason I am posting here is because I know that eventually whatever method I choose will be used for very large data sets, and I want to avoid a potential problem down the road.我知道对于小数据集,性能和内存使用可能可以忽略不计,但我在这里发布的原因是因为我知道最终我选择的任何方法都将用于非常大的数据集,我想避免潜在的问题。

Currently, I am planning to use one of the methods for a 5 by 8 by X array.目前,我计划对 5 x 8 x X 数组使用其中一种方法。 I initialized each of the methods below to be 2 by 2 by X, to make it easier to see where items are added.我将下面的每个方法初始化为 2 x 2 x X,以便更容易查看添加项目的位置。

The three methods are as follows:这三种方法如下:

Class array nesting - array of items stored in a class that is an array inside another class.类数组嵌套 - 存储在一个类中的项目数组,该类是另一个类中的数组。

public static Tier3[] _Tier3 = new Tier3[2]

3D full jagged array - an array that is expandable in all three dimensions. 3D 完整锯齿状阵列 - 可在所有三个维度上展开的阵列。

public static Item[][][] _3DjaggedItems = new Item[2][][]

2D rectangular array - a 2D array set in 2 dimensions storing jagged arrays. 2D 矩形数组 - 一个二维数组,用于存储锯齿状数组。

public static Item[,][] _2Drectangle_3DjaggedItems = new Item[2,2][]

If you know a better method of storing and accessing this struct, please keep in mind that you must be able to print out "Hello, world!"如果您知道存储和访问此结构体的更好方法,请记住您必须能够打印出“Hello, world!”。 by assigning values to通过赋值给

public ItemSystem.Item _Item;
public ItemSystem.Item[] _ItemList;

and calling PrintTest();并调用 PrintTest(); to print the message.打印消息。

I'll do my best to answer any questions.我会尽力回答任何问题。 Thank you in advance for any help!预先感谢您的任何帮助!

Below is the test code I wrote using all three methods to print out "Hello, world!".下面是我使用所有三种方法编写的测试代码,用于打印“Hello, world!”。 Just copy paste into C# compiler of choice to run.只需将粘贴复制到所选的 C# 编译器中即可运行。 I used http://rextester.com/我用过http://rextester.com/

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
public class ItemSystem
{
    public struct Item
    {
        public string _1;
        public string _2;
        public string _3;
    }

    public class Tier2
    {
        public Item[] _Items;
    }

    public class Tier3
    {
        public Tier2[] _Tier2;
    }

    //The class of containers the data of which is initialized once.
    public static Tier3[] _Tier3 = new Tier3[2]
    {
        new Tier3()
        {
            _Tier2 = new Tier2[2]
            {
                new Tier2()
                {
                    _Items = new ItemSystem.Item[]
                    {
                        new Item() { _1 = "H", _2 = "e", _3 = "l" },
                        new Item() { _1 = "l", _2 = "o", _3 = "," },
                        new Item() { _1 = " ", _2 = "w", _3 = "o" },
                        new Item() { _1 = "r", _2 = "l", _3 = "d" },
                        new Item() { _1 = "!", _2 = "", _3 = ""   }
                    }
                },
                new Tier2()
                {
                    _Items = new ItemSystem.Item[]
                    {
                        new Item() { _1 = "H", _2 = "e", _3 = "l" }
                    }
                },
            }
        },
        new Tier3()
        {
            _Tier2 = new Tier2[2]
            {
                new Tier2()
                {
                    _Items = new ItemSystem.Item[]
                    {
                        new Item() { _1 = "H", _2 = "e", _3 = "l" }
                    }
                },
                new Tier2()
                {
                    _Items = new ItemSystem.Item[]
                    {
                        new Item() { _1 = "H", _2 = "e", _3 = "l" }
                    }
                },
            }
        },
    };

    public static Item[][][] _3DjaggedItems = new Item[2][][]
    {
        new Item[2][]
        {
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" },
                new Item() { _1 = "l", _2 = "o", _3 = "," },
                new Item() { _1 = " ", _2 = "w", _3 = "o" },
                new Item() { _1 = "r", _2 = "l", _3 = "d" },
                new Item() { _1 = "!", _2 = "", _3 = ""   }
            },
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" }
            },
        },
        new Item[2][]
        {
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" }
            },
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" }
            },
        },
    };

    public static Item[,][] _2Drectangle_3DjaggedItems = new Item[2,2][]
    {
        {
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" },
                new Item() { _1 = "l", _2 = "o", _3 = "," },
                new Item() { _1 = " ", _2 = "w", _3 = "o" },
                new Item() { _1 = "r", _2 = "l", _3 = "d" },
                new Item() { _1 = "!", _2 = "", _3 = ""   }
            },
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" },
            },
        },
        {
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" },
            },
            new Item[]
            {
                new Item() { _1 = "H", _2 = "e", _3 = "l" },
            },
        },
    };
}

public class Program
{
    public ItemSystem.Item _Item;
    public ItemSystem.Item[] _ItemList;
    public int _Tier3Accessor = 0;
    public int _Tier2Accessor = 0;
    public void TestFunc()
    {
        _Item     = ItemSystem._Tier3[_Tier3Accessor]._Tier2[_Tier2Accessor]._Items[0];
        _ItemList = ItemSystem._Tier3[_Tier3Accessor]._Tier2[_Tier2Accessor]._Items;

        PrintTest();

        _Item     = ItemSystem._3DjaggedItems[_Tier3Accessor][_Tier2Accessor][0];
        _ItemList = ItemSystem._3DjaggedItems[_Tier3Accessor][_Tier2Accessor];

        PrintTest();

        _Item     = ItemSystem._2Drectangle_3DjaggedItems[_Tier3Accessor, _Tier2Accessor][0];
        _ItemList = ItemSystem._2Drectangle_3DjaggedItems[_Tier3Accessor, _Tier2Accessor];

        PrintTest();
    }

    public void PrintTest()
    {
        for(int i = 0; i < _ItemList.Length; i++)
        {
            Console.Write(_ItemList[i]._1);
            Console.Write(_ItemList[i]._2);
            Console.Write(_ItemList[i]._3);
        }

        Console.WriteLine("\n");
    }

    public static void Main(string[] args)
    {
        Program p = new Program();
        p.TestFunc();
    }
}
}

Figured it out.弄清楚了。 Did some more searching, and after a while came across these posts进行了更多搜索,一段时间后发现了这些帖子

What are the differences between a multidimensional array and an array of arrays in C#? C#中的多维数组和数组数组有什么区别?

Why are multi-dimensional arrays in .NET slower than normal arrays? 为什么 .NET 中的多维数组比普通数组慢?

The short answer is that flattening arrays and then calculating the index you want to access is the fastest way to go.简短的回答是展平数组然后计算您要访问的索引是最快的方法。 Followed by Jagged arrays, because the Multi Dimensional arrays have a slow implementation in C#.其次是锯齿状数组,因为多维数组在 C# 中的实现速度很慢。

In terms of formatting, Jagged arrays also allow you to keep a bit more organised due to how they need to be initialized.在格式化方面,锯齿状数组还允许您根据它们需要如何初始化而保持更有条理。 You could do the same with a flattened array, but you would need to get creative with spacing and commenting to make sure everything is where it should be visually for easy reading.您可以对扁平数组执行相同操作,但您需要在间距和注释方面发挥创意,以确保所有内容都在视觉上易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM