Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Immutable;
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Jobs;
- using BenchmarkDotNet.Running;
- namespace Benchmark
- {
- [SimpleJob(RuntimeMoniker.Net50)]
- public class ImmutableArrayTest
- {
- [Params(10, 100, 1000)]
- public int ProblemSize;
- private ImmutableArray<int> _data;
- [GlobalSetup]
- public void Setup()
- {
- var random = new Random();
- var builder = ImmutableArray.CreateBuilder<int>(ProblemSize);
- for (int i = 0; i < ProblemSize; ++i)
- {
- builder.Add(random.Next());
- }
- _data = builder.MoveToImmutable();
- }
- static int ViaRef(in ImmutableArray<int> array)
- {
- int sum = 0;
- foreach (var i in array)
- {
- sum += i;
- }
- return sum;
- }
- static int ViaValue(ImmutableArray<int> array)
- {
- int sum = 0;
- foreach (var i in array)
- {
- sum += i;
- }
- return sum;
- }
- [Benchmark]
- public double Ref()
- {
- return ViaRef(_data);
- }
- [Benchmark]
- public double Value()
- {
- return ViaValue(_data);
- }
- }
- [SimpleJob(RuntimeMoniker.Net50)]
- public class HeavyStruct
- {
- readonly struct Heavy
- {
- public double A { init; get; }
- public double B { init; get; }
- public double C { init; get; }
- public double D { init; get; }
- public double E { init; get; }
- }
- private Heavy data;
- [GlobalSetup]
- public void Setup()
- {
- var random = new Random();
- data = new Heavy
- {
- A = random.NextDouble(),
- B = random.NextDouble(),
- C = random.NextDouble(),
- D = random.NextDouble(),
- E = random.NextDouble(),
- };
- }
- static double ViaRef(in Heavy array)
- {
- double sum
- = array.A +
- array.B +
- array.C +
- array.D +
- array.E
- ;
- return sum;
- }
- static double ViaValue(Heavy array)
- {
- double sum
- = array.A +
- array.B +
- array.C +
- array.D +
- array.E
- ;
- return sum;
- }
- [Benchmark]
- public double Ref()
- {
- return ViaRef(data);
- }
- [Benchmark]
- public double Value()
- {
- return ViaValue(data);
- }
- }
- [SimpleJob(RuntimeMoniker.Net50)]
- public class VeryHeavyStruct
- {
- readonly struct Heavy
- {
- public double A { init; get; }
- public double B { init; get; }
- public double C { init; get; }
- public double D { init; get; }
- public double E { init; get; }
- public double A1 { init; get; }
- public double B1 { init; get; }
- public double C1 { init; get; }
- public double D1 { init; get; }
- public double E1 { init; get; }
- }
- private Heavy data;
- [GlobalSetup]
- public void Setup()
- {
- var random = new Random();
- data = new Heavy
- {
- A = random.NextDouble(),
- B = random.NextDouble(),
- C = random.NextDouble(),
- D = random.NextDouble(),
- E = random.NextDouble(),
- A1 = random.NextDouble(),
- B1 = random.NextDouble(),
- C1 = random.NextDouble(),
- D1 = random.NextDouble(),
- E1 = random.NextDouble(),
- };
- }
- static double ViaRef(in Heavy array)
- {
- double sum
- = array.A +
- array.B +
- array.C +
- array.D +
- array.E +
- array.A1 +
- array.B1 +
- array.C1 +
- array.D1 +
- array.E1
- ;
- return sum;
- }
- static double ViaValue(Heavy array)
- {
- double sum
- = array.A +
- array.B +
- array.C +
- array.D +
- array.E +
- array.A1 +
- array.B1 +
- array.C1 +
- array.D1 +
- array.E1
- ;
- return sum;
- }
- [Benchmark]
- public double Ref()
- {
- return ViaRef(data);
- }
- [Benchmark]
- public double Value()
- {
- return ViaValue(data);
- }
- }
- public static class Program
- {
- public static void Main()
- {
- BenchmarkRunner.Run<VeryHeavyStruct>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement