---
# System prepended metadata

title: Class Struct in Structを列挙したときのパフォーマンス計測

---

# Class Struct in Structを列挙したときのパフォーマンス計測

## 環境
.net7 window10
core i7-8700
memory 

## ソースコード
```csharp=
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;

namespace Benchmark_DataType
{
    [MemoryDiagnoser]
    //[ShortRunJob]
    [MinColumn, MaxColumn]
    public class DataSizeBenchmark3
    {
        private const int ArraySize = 1024 * 16;

        private readonly Struct1[] struct1 = new Struct1[ArraySize];
        private readonly Struct2[] struct2 = new Struct2[ArraySize];
        private readonly Struct4[] struct4 = new Struct4[ArraySize];
        private readonly Struct8[] struct8 = new Struct8[ArraySize];
        private readonly Struct16[] struct16 = new Struct16[ArraySize];
        private readonly Struct32[] struct32= new Struct32[ArraySize];
        private readonly Struct64[] struct64 = new Struct64[ArraySize];
        private readonly Struct128[] struct128 = new Struct128[ArraySize];

        private readonly Class1[] class1 = new Class1[ArraySize];
        private readonly Class2[] class2 = new Class2[ArraySize];
        private readonly Class4[] class4 = new Class4[ArraySize];
        private readonly Class8[] class8 = new Class8[ArraySize];
        private readonly Class16[] class16 = new Class16[ArraySize];
        private readonly Class32[] class32= new Class32[ArraySize];
        private readonly Class64[] class64 = new Class64[ArraySize];
        private readonly Class128[] class128 = new Class128[ArraySize];
        public DataSizeBenchmark3()
        {
            for (int i = 0; i < ArraySize; i++)
            {
                struct1[i] = new Struct1();
                struct2[i] = new Struct2();
                struct4[i] = new Struct4();
                struct8[i] = new Struct8();
                struct16[i] = new Struct16();
                struct32[i] = new Struct32();
                struct64[i] = new Struct64();
                struct128[i] = new Struct128();

                class1[i] = new Class1();
                class2[i] = new Class2();
                class4[i] = new Class4();
                class8[i] = new Class8();
                class16[i] = new Class16();
                class32[i] = new Class32();
                class64[i] = new Class64();
                class128[i] = new Class128();
            }
        }
        [Benchmark]
        public long Struct1()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct1[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct1()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct1[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class1()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class1[i]);
            }
            return sum;
        }

        [Benchmark]
        public long Struct2()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct2[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct2()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct2[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class2()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class2[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct4()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct4[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct4()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct4[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class4()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class4[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct8()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct8[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct8()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct8[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class8()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class8[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct16()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct16[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct16()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct16[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class16()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class16[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct32()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct32[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct32()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct32[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class32()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class32[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct64()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct64[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct64()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct64[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class64()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class64[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Struct128()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(struct128[i]);
            }
            return sum;
        }
        [Benchmark]
        public long InStruct128()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(in struct128[i]);
            }
            return sum;
        }
        [Benchmark]
        public long Class128()
        {
            var sum = 0L;
            for (int i = 0; i < ArraySize; i++)
            {
                sum += Sum(class128[i]);
            }
            return sum;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct1 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct1 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Class1 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct2 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct2 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Class2 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct4 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct4 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Class4 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct8 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct8 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Class8 v)
        {
            return v.a + v.a + v.a + v.a;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct16 v)
        {
            return v.a + v.b + v.a + v.b;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct16 v)
        {
            return v.a + v.b + v.a + v.b;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Class16 v)
        {
            return v.a + v.b + v.a + v.b;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct32 v)
        {
            return v.a + v.b + v.c + v.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct32 v)
        {
            return v.a + v.b + v.c + v.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Class32 v)
        {
            return v.a + v.b + v.c + v.d;
        }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct64 v)
        {
            return v.a + v.b + v.c + v.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct64 @struct)
        {
            return @struct.a + @struct.b + @struct.c + @struct.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Class64 v)
        {
            return v.a + v.b + v.c + v.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Struct128 v)
        {
            return v.a + v.b + v.c + v.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(in Struct128 @struct)
        {
            return @struct.a + @struct.b + @struct.c + @struct.d;
        }
        [MethodImpl(MethodImplOptions.NoOptimization)]
        public long Sum(Class128 v)
        {
            return v.a + v.b + v.c + v.d;
        }
    }

    public readonly struct Struct1
    {
        public readonly byte a;
        public Struct1()
        {
            a = (byte)Random.Shared.Next();
        }
    }
    public readonly struct Struct2
    {
        public readonly short a;
        public Struct2()
        {
            a = (short)Random.Shared.Next();
        }
    }
    public readonly struct Struct4
    {
        public readonly int a;
        public Struct4()
        {
            a = Random.Shared.Next();
        }
    }
    public readonly struct Struct8
    {
        public readonly long a;
        public Struct8()
        {
            a = Random.Shared.NextInt64();
        }
    }
    public readonly struct Struct16
    {
        public readonly long a;
        public readonly long b;
        public Struct16()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
        }
    }
    public readonly struct Struct32
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;

        public Struct32()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
        }
    }
    public readonly struct Struct64
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;
        public readonly long e;
        public readonly long f;
        public readonly long g;
        public readonly long h;

        public Struct64()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
            e = Random.Shared.NextInt64();
            f = Random.Shared.NextInt64();
            g = Random.Shared.NextInt64();
            h = Random.Shared.NextInt64();
        }
    }
    public readonly struct Struct128
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;

        public readonly long e;
        public readonly long f;
        public readonly long g;
        public readonly long h;

        public readonly long i;
        public readonly long j;
        public readonly long k;
        public readonly long l;

        public readonly long m;
        public readonly long n;
        public readonly long o;
        public readonly long p;

        public Struct128()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
            e = Random.Shared.NextInt64();
            f = Random.Shared.NextInt64();
            g = Random.Shared.NextInt64();
            h = Random.Shared.NextInt64();
            i = Random.Shared.NextInt64();
            j = Random.Shared.NextInt64();
            k = Random.Shared.NextInt64();
            l = Random.Shared.NextInt64();
            m = Random.Shared.NextInt64();
            n = Random.Shared.NextInt64();
            o = Random.Shared.NextInt64();
            p = Random.Shared.NextInt64();
        }
    }
    public class Class1
    {
        public readonly byte a;
        public Class1()
        {
            a = (byte)Random.Shared.Next();
        }
    }
    public class Class2
    {
        public readonly short a;
        public Class2()
        {
            a = (short)Random.Shared.Next();
        }
    }
    public class Class4
    {
        public readonly int a;
        public Class4()
        {
            a = Random.Shared.Next();
        }
    }
    public class Class8
    {
        public readonly long a;
        public Class8()
        {
            a = Random.Shared.NextInt64();
        }
    }
    public class Class16
    {
        public readonly long a;
        public readonly long b;
        public Class16()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
        }
    }
    public class Class32
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;

        public Class32()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
        }
    }
    public class Class64
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;
        public readonly long e;
        public readonly long f;
        public readonly long g;
        public readonly long h;

        public Class64()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
            e = Random.Shared.NextInt64();
            f = Random.Shared.NextInt64();
            g = Random.Shared.NextInt64();
            h = Random.Shared.NextInt64();
        }
    }
    public class Class128
    {
        public readonly long a;
        public readonly long b;
        public readonly long c;
        public readonly long d;

        public readonly long e;
        public readonly long f;
        public readonly long g;
        public readonly long h;

        public readonly long i;
        public readonly long j;
        public readonly long k;
        public readonly long l;

        public readonly long m;
        public readonly long n;
        public readonly long o;
        public readonly long p;

        public Class128()
        {
            a = Random.Shared.NextInt64();
            b = Random.Shared.NextInt64();
            c = Random.Shared.NextInt64();
            d = Random.Shared.NextInt64();
            e = Random.Shared.NextInt64();
            f = Random.Shared.NextInt64();
            g = Random.Shared.NextInt64();
            h = Random.Shared.NextInt64();
            i = Random.Shared.NextInt64();
            j = Random.Shared.NextInt64();
            k = Random.Shared.NextInt64();
            l = Random.Shared.NextInt64();
            m = Random.Shared.NextInt64();
            n = Random.Shared.NextInt64();
            o = Random.Shared.NextInt64();
            p = Random.Shared.NextInt64();
        }
    }
}

```

## 計測結果
![image](https://hackmd.io/_uploads/ByMwA4ePT.png)
![image](https://hackmd.io/_uploads/rJmdC4gv6.png)
