A game about forced loneliness, made by TACStudios
1using NUnit.Framework;
2using System;
3using Unity.Burst;
4using Unity.Collections;
5using Unity.Collections.LowLevel.Unsafe;
6using Unity.Jobs;
7using Unity.PerformanceTesting;
8
9namespace Unity.Collections.PerformanceTests
10{
11 internal class UnsafeListPerformanceTests
12 {
13 private struct TestStruct
14 {
15 public int x;
16 public short y;
17 public bool z;
18 }
19
20 [Test, Performance]
21 [Category("Performance")]
22 public unsafe void UnsafeUtility_ReadArrayElement_Performance()
23 {
24 const int numElements = 16 << 10;
25
26 var list = new UnsafeList<TestStruct>(numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory);
27
28 for (int i = 0; i < numElements; ++i)
29 {
30 list.Add(new TestStruct { x = i, y = (short)(i + 1), z = true });
31 }
32
33 Measure.Method(() =>
34 {
35 for (int i = 0; i < numElements; ++i)
36 {
37 UnsafeUtility.ReadArrayElement<TestStruct>(list.Ptr, i);
38 }
39 })
40 .WarmupCount(100)
41 .MeasurementCount(1000)
42 .Run();
43
44 list.Dispose();
45 }
46
47 [Test, Performance]
48 [Category("Performance")]
49 public unsafe void UnsafeUtility_ReadArrayElementBoundsChecked_Performance()
50 {
51 const int numElements = 16 << 10;
52
53 var list = new UnsafeList<TestStruct>(numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory);
54
55 for (int i = 0; i < numElements; ++i)
56 {
57 list.Add(new TestStruct { x = i, y = (short)(i + 1), z = true });
58 }
59
60 Measure.Method(() =>
61 {
62 for (int i = 0; i < numElements; ++i)
63 {
64 UnsafeUtilityExtensions.ReadArrayElementBoundsChecked<TestStruct>(list.Ptr, i, numElements);
65 }
66 })
67 .WarmupCount(100)
68 .MeasurementCount(1000)
69 .Run();
70
71 list.Dispose();
72 }
73
74 [Test, Performance]
75 [Category("Performance")]
76 public unsafe void UnsafeUtility_WriteArrayElement_Performance()
77 {
78 const int numElements = 16 << 10;
79
80 var list = new UnsafeList<TestStruct>(numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory);
81
82 var test = new TestStruct { x = 0, y = 1, z = true };
83 Measure.Method(() =>
84 {
85 for (int i = 0; i < numElements; ++i)
86 {
87 UnsafeUtility.WriteArrayElement(list.Ptr, i, test);
88 }
89 })
90 .WarmupCount(100)
91 .MeasurementCount(1000)
92 .Run();
93
94 list.Dispose();
95 }
96
97 [Test, Performance]
98 [Category("Performance")]
99 public unsafe void UnsafeUtility_WriteArrayElementBoundsChecked_Performance()
100 {
101 const int numElements = 16 << 10;
102
103 var list = new UnsafeList<TestStruct>(numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory);
104
105 var test = new TestStruct { x = 0, y = 1, z = true };
106 Measure.Method(() =>
107 {
108 for (int i = 0; i < numElements; ++i)
109 {
110 UnsafeUtilityExtensions.WriteArrayElementBoundsChecked(list.Ptr, i, test, numElements);
111 }
112 })
113 .WarmupCount(100)
114 .MeasurementCount(1000)
115 .Run();
116
117 list.Dispose();
118 }
119 }
120}