最佳/最快的方式来改变一个矩阵/访问元素 [英] Best/Fastest Way To Change/Access Elements of a Matrix

查看:153
本文介绍了最佳/最快的方式来改变一个矩阵/访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的C#和我有很难与我们的数组,数组,数组锯齿,矩阵和东西阵列。这是从C ++完全不同,因为我不能得到一个参考(除非我用不安全code)到矩阵的一行,使用指针和东西。

I'm quite new to C# and I'm having difficult with our arrays, arrays of arrays, jagged arrays, matrixes and stuff. It's quite different from the C++ , since I can't get a reference (unless I use unsafe code) to a row of a matrix, using pointers and stuff.

总之,这里有一个问题:我有一个名为映像,cointains 1024列和768线的结构/类。对于每一行/列那里有一个像素结构/类,它包含3个字节。我想获得在矩阵的随机的地方/组像素尽可能快的。

Anyway, here's the problem: I have a struct/class called "Image" that cointains 1024 columns and 768 lines. For each line/column theres a 'pixel' struct/class that contains 3 bytes. I'd like to get/set pixels in random places of the matrix as fast as possible.

让我们pretend我有25个像素矩阵。这是5行5列,这样的事情:

Let's pretend I have a matrix with 25 pixels. That is 5 rows and 5 columns, something like this:

A B C D E
F G H I J
K L M N O
P Q R S T 
U V X W Y

和我需要M M比较H与R.再到L和N。然后,我需要'之'G + H + I + L + M + N + Q + R + S

And I need to compare M to H and R. Then M to L and N. Then I need to 'sum' G+H+I+L+M+N+Q+R+S.

我怎么能这样做?

可能性:
1)建立类似的像素[5] [5](这是一个交错数组,对吧?),这将是缓慢的,每当我尝试在不同的列比较元素,对不对?
2)建立类似的像素[25],该不会是那么容易code /准备,因为我需要做一些(简单)的数学我想访问的每个元素和一切
3)创建类似PIXE [5,5](这是一个多维数组,对吧?)......但我不知道怎么会被转换为实际的内存...如果它的将是一个单一的块的存储器,像PIXE [25],或什么...

Possibilities: 1) Create something like pixel[5][5] (that's a jagged array, right?), which will be slow whenever I try to compare elements on different columns, right? 2) Create something like pixel[25] , which won't be as easy to code/ready because I'll need to do some (simple) math each and everything I want to access a element 3) Create something like pixe[5,5] (that's a multi-dimensional array, right?)... But I don't know how that will be translated to actual memory... If it's going to be a single block of memory, like the pixe[25], or what...

由于我打算做这个操作(随机总和/元素的比较是在不同的行/列)数万每个图像倍。我有1000+ imagens。 code优化是必须的。不幸的是我不知道我应该使用哪种结构/ CLASSE。

Since I intend to do this operations ('random' sums/comparison of elements that are in different rows/columns) tens of thousands of times per image. And I have 1000+ imagens. Code optimization is a must... Sadly I'm not sure which structure / classe I should use.

TL; DR:什么最快和最新获取/设置的(固定大小)矩阵的随机位置元素的最简单的(编码WISE)的方式

编辑:我不想比较C ++到C#。我只是说我是新的C#和我想找到做到这一点的最好办法,使用C#。请不要告诉我回去C ++。

edit: I do not want to compare C++ to C#. I'm just saying I AM NEW TO C# and I'd like to find the best way to accomplish this, using C#. Please don't tell me to go back to C++.

推荐答案

我刚刚完成的测试,继承人的结果是:

I just finished testing, heres the result:

SD Array Test1: 00:00:00.9388379
SD Array Test2: 00:00:00.4117926
MD Array Test1: 00:00:01.4977765
MD Array Test2: 00:00:00.8950093
Jagged Array Test1: 00:00:03.6850013
Jagged Array Test2: 00:00:00.5036041

结论:一维数组是要走的路......可悲的是,我们失去可读性

Conclusion: Single dimensional array is the way to go... Sadly we lose in readability.

和继承人的code:

    int[] myArray = new int[10000 * 10000];

    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            myArray[(i*10000)+j] = i+j;
        }
    }



    Stopwatch sw = new Stopwatch();
    int sum = 0;

    sw.Start();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += myArray[(j * 10000) + i];
        }
    }
    sw.Stop();

    Console.WriteLine("SD Array Test1: " + sw.Elapsed.ToString());
    sum=0;

    sw.Restart();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += myArray[(i * 10000) + j];
        }
    }
    sw.Stop();

    Console.WriteLine("SD Array Test2: " + sw.Elapsed.ToString());

    myArray = null;

    int[,] MDA = new int[10000, 10000];
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            MDA[i, j] = i + j;
        }
    }

    sum = 0;
    sw.Restart();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += MDA[j, i];
        }
    }
    sw.Stop();
    Console.WriteLine("MD Array Test1: " + sw.Elapsed.ToString());

    sw.Restart();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += MDA[i, j];
        }
    }
    sw.Stop();
    Console.WriteLine("MD Array Test2: " + sw.Elapsed.ToString());
    MDA = null;

    int[][] JA = new int[10000][];
    for (int i = 0; i < 10000; i++)
    {
        JA[i] = new int[10000];
    }
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            JA[i][j] = i + j;
        }
    }

    sum = 0;
    sw.Restart();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += JA[j][i];
        }
    }
    sw.Stop();
    Console.WriteLine("Jagged Array Test1: " + sw.Elapsed.ToString());

    sw.Restart();
    for (int i = 0; i < 10000; i++)
    {
        for (int j = 0; j < 10000; j++)
        {
            sum += JA[i][j];
        }
    }
    sw.Stop();
    Console.WriteLine("Jagged Array Test2: " + sw.Elapsed.ToString());
    MDA = null;

    Console.ReadKey();

这篇关于最佳/最快的方式来改变一个矩阵/访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆