Newer
Older
DungeonShooting / DungeonShooting_Godot / src / test / TestGridData.cs
@小李xl 小李xl on 24 Nov 2023 2 KB 制作消除效果中
  1. using Godot;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. public partial class TestGridData : Node2D
  6. {
  7.  
  8. public class TestGrid<T>
  9. {
  10. public TestGrid(int width)
  11. {
  12. Width = width;
  13. }
  14.  
  15. public int Width { get; }
  16. private Dictionary<int, T> _dictionary = new Dictionary<int, T>();
  17.  
  18. public void Set(int x, int y, T data)
  19. {
  20. if (x <= Width)
  21. {
  22. _dictionary[y * Width + x] = data;
  23. }
  24. }
  25.  
  26. public T Get(int x, int y)
  27. {
  28. if (x <= Width && _dictionary.TryGetValue(y * Width + x, out var value))
  29. {
  30. return value;
  31. }
  32.  
  33. return default;
  34. }
  35.  
  36. public void ForEach(Action<int, int, T> callback)
  37. {
  38. foreach (var keyValuePair in _dictionary)
  39. {
  40. var index = keyValuePair.Key;
  41. callback(index % Width, index / Width, keyValuePair.Value);
  42. }
  43. }
  44. }
  45.  
  46. public override void _Ready()
  47. {
  48. var time = DateTime.Now;
  49. var testGrid = new TestGrid<int>(100000);
  50. for (int i = 0; i < 1000; i++)
  51. {
  52. for (int j = 0; j < 1000; j++)
  53. {
  54. testGrid.Set(i, j, i + j);
  55. }
  56. }
  57. Debug.Log("TestGrid设置值用时: " + (DateTime.Now - time).Milliseconds);
  58.  
  59. time = DateTime.Now;
  60. var testGrid2 = new InfiniteGrid<int>();
  61. for (int i = 0; i < 1000; i++)
  62. {
  63. for (int j = 0; j < 1000; j++)
  64. {
  65. testGrid2.Set(i, j, i + j);
  66. }
  67. }
  68. Debug.Log("Grid设置值用时: " + (DateTime.Now - time).Milliseconds);
  69. time = DateTime.Now;
  70. for (int i = 0; i < 1000; i++)
  71. {
  72. for (int j = 0; j < 1000; j++)
  73. {
  74. testGrid.Get(i, j);
  75. }
  76. }
  77. Debug.Log("TestGrid取值用时: " + (DateTime.Now - time).Milliseconds);
  78.  
  79. time = DateTime.Now;
  80. for (int i = 0; i < 1000; i++)
  81. {
  82. for (int j = 0; j < 1000; j++)
  83. {
  84. testGrid2.Get(i, j);
  85. }
  86. }
  87. Debug.Log("Grid取值用时: " + (DateTime.Now - time).Milliseconds);
  88. time = DateTime.Now;
  89. testGrid.ForEach((i, i1, arg3) =>
  90. {
  91. });
  92. Debug.Log("TestGrid遍历用时: " + (DateTime.Now - time).Milliseconds);
  93. time = DateTime.Now;
  94. testGrid2.ForEach((i, i1, arg3) =>
  95. {
  96. });
  97. Debug.Log("Grid遍历用时: " + (DateTime.Now - time).Milliseconds);
  98. }
  99. }