Access time for small array versus small structure in C #

I need to process about 500,000 data points, each of which consists of 4 decimal places. I would like to use an array of structures for this. Will it be much slower than using an array of arrays? It seems that memory will not be a problem, but there will be speed - it should be fast.

A quick code example of two options:

Option 1:

public struct Struct
{
    public decimal A { get; set; }
    public decimal B { get; set; }
    public decimal C { get; set; }
    public decimal D { get; set; }
}

Using:

private Struct[] data;

Option 2:

private decimal [][] data;

Also using the decimalcorrect data type to use? Data points are money ...

Thanks! Brian

+3
source share
6 answers

A, B, C, D , structs - , ( ) . A, B .., , .

, , . , , , , , .

+4

:

( ) , , , .

, .

+2
  • - , , .
  • .

, , (, ), . , C 4 , :

MyStruct val = array[5];
val.C = newValue;
array[5] = val;

, . , ...

+1

hmm... , :

private Struct[] data = new Struct[x];
private decimal[,] data = new decimal[x,4];

...

+1

, , .

+1

Struct , .

public struct Struct
{
    // Unless you're filling your get/set blocks with anything,
    // these properties will be in-lined in compilation time
    // and will have the same performance/behavior as using public fields

    public decimal A { get; set; }
    public decimal B { get; set; }
    public decimal C { get; set; }
    public decimal D { get; set; }
}

. , , .

, . decimal - 128- , , . - , , . , . , float.

, 32- ( ), .

, !

+1

Source: https://habr.com/ru/post/1715648/


All Articles