暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UnsafeListPerformanceTests.cs 3.7KB

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