-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBurstFunctionTest.cs
299 lines (243 loc) · 9.01 KB
/
BurstFunctionTest.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Assets.UnityXBurst.Helpers;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace Assets
{
public class BurstFunctionTest : MonoBehaviour
{
public unsafe void Start()
{
var timer = new Timer();
for (int i = 0; i < 1000; i++)
{
if (i == 20) timer.IsEnabled = true;
using (var input1 = new NativeArray<int>(100000, Allocator.TempJob))
using (var input2 = new NativeArray<int>(100000, Allocator.TempJob))
{
timer.Restart();
timer.Stop();
timer.Save("Timer No Work Baseline");
var sw1 = new Stopwatch();
sw1.Start();
sw1.Stop();
timer.Timings.Add(("StopWatch No Work Baseline", sw1.Elapsed));
// -----------------------------------
// Standard C# method.
var instance = new MyFunctions.AddIntegersCSharp();
timer.Restart();
var result0 = instance.Execute(5, 7);
timer.Stop();
timer.Save("AddIntegers(PureC#)");
// -----------------------------------
// Normal Burst Job construction.
int intResult1 = 0;
var addIntegersJobWithResult = new MyFunctions.AddIntegersJobWithResult
{
arg1 = 5,
arg2 = 7,
resultPtr = &intResult1
};
timer.Restart();
addIntegersJobWithResult.Schedule().Complete();
timer.Stop();
timer.Save("AddIntegersJob(NonBurst): with result");
// -----------------------------------
// Normal Burst job without a return result;
var addIntegersJob = new MyFunctions.AddIntegersJob
{
arg1 = 5,
arg2 = 7,
};
timer.Restart();
addIntegersJob.Schedule().Complete();
timer.Stop();
timer.Save("AddIntegersJob(NonBurst): no result");
// -----------------------------------
// Function in burst, maintains references, uses GetAddress/CopyPtrToStructure.
timer.Restart();
var intResult3 = MyFunctions.AddIntegersFuncX2.Invoke(5, 7);
timer.Stop();
timer.Save("AddIntegersFunc(Burst):");
// -----------------------------------
// Uses no jobs code, just pure c# performing the same logic.
var instanceArr = new MyFunctions.ArrayTestCSharp();
timer.Restart();
instanceArr.Execute(input1, input2);
timer.Stop();
timer.Save("ArrayTest(PureC#)");
// -----------------------------------
// Uses the Function Job
NativeArray<int> arrResult;
timer.Restart();
arrResult = MyFunctions.ArrayTestFunc.Invoke(input1, input2);
timer.Stop();
timer.Save("ArrayTestFunc(Burst)");
// -----------------------------------
// Uses a standard Job construction, same as Function version except it has no result.
var compiledArr = new MyFunctions.ArrayTestJob
{
arg1 = input1,
arg2 = input2,
};
timer.Restart();
compiledArr.Schedule().Complete();
timer.Stop();
timer.Save("ArrayTestJob(Burst):ScheduleComplete");
// -----------------------------------
// Uses a standard Job construction fired with Run(); returns no result.
compiledArr = new MyFunctions.ArrayTestJob
{
arg1 = input1,
arg2 = input2,
};
timer.Restart();
compiledArr.Run();
timer.Stop();
timer.Save("ArrayTestJob(Burst):Run");
}
}
timer.LogAverages();
}
}
public class Timer : IEnumerable<(string, TimeSpan)>
{
public Stopwatch Stopwatch = new Stopwatch();
public List<(string, TimeSpan)> Timings = new List<(string, TimeSpan)>();
public void Start() => Stopwatch.Start();
public void Restart() => Stopwatch.Restart();
public void Stop() => Stopwatch.Stop();
public TimeSpan Elapsed => Stopwatch.Elapsed;
public void Save(string name)
{
if (IsEnabled)
{
Timings.Add((name, Stopwatch.Elapsed));
}
}
public void Save(string name, TimeSpan time)
{
if (IsEnabled)
{
Timings.Add((name, time));
}
}
public void LogAverages()
{
foreach (var group in Timings.GroupBy(t => t.Item1))
{
Debug.Log($"{group.Key}: {group.Average(t => t.Item2.TotalMilliseconds):N8} ms");
}
}
public bool IsEnabled { get; set; }
public IEnumerator<(string, TimeSpan)> GetEnumerator() => Timings.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class MyFunctions
{
public struct AddIntegersCSharp
{
public int Execute(int a, int b)
{
return a + b;
}
}
[BurstCompile]
public struct AddIntegersJob : IJob
{
public int arg1;
public int arg2;
public void Execute()
{
arg1 = arg1 + arg2;
}
}
[BurstCompile]
public struct AddIntegersJobWithResult : IJob
{
public int arg1;
public int arg2;
[NativeDisableUnsafePtrRestriction]
public unsafe int* resultPtr;
public unsafe void Execute()
{
ref var result = ref *resultPtr;
result = arg1 + arg2;
}
}
[BurstCompile]
public struct ArrayTestJob : IJob
{
public NativeArray<int> arg1;
public NativeArray<int> arg2;
public void Execute()
{
for (int i = 0; i < 100000; i++)
{
arg1[i] = i;
}
for (int i = 0; i < 100000; i++)
{
arg2[i] = arg1[i];
}
}
}
[BurstCompile]
public struct AddIntegersFuncX2 : IBurstFunction<int, int, int>
{
public int Execute(int a, int b)
{
return a + b;
}
public static int Invoke(int a, int b)
{
return BurstFunction<AddIntegersFuncX2, int, int, int>.Run(Instance, a, b);
}
private static readonly AddIntegersFuncX2 Instance = new AddIntegersFuncX2();
}
[BurstCompile]
public struct ArrayTestFunc : IBurstFunction<NativeArray<int>, NativeArray<int>, NativeArray<int>>
{
public NativeArray<int> Execute(NativeArray<int> arg1, NativeArray<int> arg2)
{
for (int i = 0; i < 100000; i++)
{
arg1[i] = i;
}
for (int i = 0; i < 100000; i++)
{
arg2[i] = arg1[i];
}
return arg2;
}
public static NativeArray<int> Invoke(NativeArray<int> a, NativeArray<int> b)
{
return BurstFunction<ArrayTestFunc, NativeArray<int>, NativeArray<int>, NativeArray<int>>.Run(Instance, a, b);
}
public static ArrayTestFunc Instance { get; } = new ArrayTestFunc();
}
public struct ArrayTestCSharp
{
public NativeArray<int> Execute(NativeArray<int> arg1, NativeArray<int> arg2)
{
for (int i = 0; i < 100000; i++)
{
arg1[i] = i;
}
for (int i = 0; i < 100000; i++)
{
arg2[i] = arg1[i];
}
return arg2;
}
}
}
}