-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
158 lines (134 loc) · 4.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace RoundedDistributionComparison5
{
internal class Program
{
private const int Count = 1_000_000;
private const long Ticks = TimeSpan.TicksPerMillisecond / 1000;
private static readonly Dictionary<DataTypes, double> Result = new(6);
public static void Main()
{
var d0 = RoundedDistribution.Create(EnumLongData);
var d1 = RoundedDistributionV2<DataTypes>.Create(EnumLongData);
Benchmark(FirstImpl);
Benchmark(NewImpl);
Benchmark(NewImplNoAllocation);
}
private static void Benchmark(Func<IDictionary<DataTypes, double>> action)
{
var total = 0.0;
var stopWatch = new Stopwatch();
IDictionary<DataTypes, double> result = null;
for (var i = 0; i < Count; i++)
{
stopWatch.Start();
result = action();
stopWatch.Stop();
total += stopWatch.ElapsedTicks;
stopWatch.Reset();
}
total /= Ticks;
Console.WriteLine($"Total: {total:#.00}{Environment.NewLine}Average: {total / Count:0.00} mcs");
Console.WriteLine(string.Join(" ", result?.Select(r => $"{r.Key}:{r.Value:0}")));
Console.WriteLine();
}
private static IDictionary<DataTypes, double> FirstImpl()
{
return RoundedDistribution.Create(EnumLongData).AsPercents();
}
private static IDictionary<DataTypes, double> NewImpl()
{
return RoundedDistributionV2<DataTypes>.Create(EnumLongData);
}
private static IDictionary<DataTypes, double> NewImplNoAllocation()
{
return RoundedDistributionV2<DataTypes>.Create(EnumLongData, Result);
}
private static readonly Dictionary<DataTypes, int> EnumIntData = new()
{
{ DataTypes.First, 146 },
{ DataTypes.Second, 123 },
{ DataTypes.Third, 323 },
{ DataTypes.Fourth, 982 },
{ DataTypes.Fifth, 457 }
};
private static readonly Dictionary<DataTypes, double> EnumDoubleData = new()
{
{ DataTypes.First, 1247.32 },
{ DataTypes.Second, 42365.13 },
{ DataTypes.Third, 32332.243 },
{ DataTypes.Fourth, 12982.112 },
{ DataTypes.Fifth, 22223.125 }
};
private static readonly Dictionary<DataTypes, long> EnumLongData = new()
{
{ DataTypes.First, 146_000_000_000 },
{ DataTypes.Second, 123_000_000_000 },
{ DataTypes.Third, 323_000_000_000 },
{ DataTypes.Fourth, 982_000_000_000 },
{ DataTypes.Fifth, 457_000_000_000 }
};
private static readonly Dictionary<int, int> IntIntData = new()
{
{ 0, 146 },
{ 1, 123 },
{ 2, 323 },
{ 3, 982 },
{ 4, 457 }
};
private static readonly Dictionary<int, double> IntDoubleData = new()
{
{ 0, 1247.32 },
{ 1, 42365.13 },
{ 2, 32332.243 },
{ 3, 12982.112 },
{ 4, 22223.125 }
};
private static readonly Dictionary<int, long> IntLongData = new()
{
{ 0, 146_000_000_000 },
{ 1, 123_000_000_000 },
{ 2, 323_000_000_000 },
{ 3, 982_000_000_000 },
{ 4, 457_000_000_000 }
};
private static readonly Dictionary<long, int> LongIntData = new()
{
{ 0, 0 },
{ 1, 1_000_000 },
{ 2, 2_000_000 },
{ 3, 6_000_000 },
{ 4, 7_000_000 },
{ 5, 12_000_000 },
{ 6, 3_000_000 }
};
private static readonly Dictionary<long, long> LongLongData = new()
{
{ 0, 146_000_000_000 },
{ 1, 123_000_000_000 },
{ 2, 323_000_000_000 },
{ 3, 982_000_000_000 },
{ 4, 457_000_000_000 }
};
private static readonly Dictionary<long, double> LongDoubleData = new()
{
{ 0, 1247.32 },
{ 1, 42365.13 },
{ 2, 32332.243 },
{ 3, 12982.112 },
{ 4, 22223.125 },
{ 5, 23156.786 }
};
}
public enum DataTypes
{
First = 0,
Second = 1,
Third = 2,
Fourth = 3,
Fifth = 4
}
}