View difference between Paste ID: JwvB6U4C and AdSKWgzp
SHOW: | | - or go back to the newest paste.
1
using Godot;
2
using System;
3
using System.Collections.Generic;
4
using System.Runtime.InteropServices;
5
6
namespace Game;
7
public partial class Main : Node2D
8
{
9
10
	#region timer
11
	private ulong _time;
12
13
	private void TimeStart()
14
	{
15
		_time = Time.GetTicksUsec();
16
	}
17
18
	private void TimeStop(string prefix = "")
19
	{
20
		string Reverse(string s)
21
		{
22
			char[] chars = s.ToCharArray();
23
			Array.Reverse(chars);
24
			return new string(chars);
25
		}
26
		string Parse(string s, bool isReverse)
27
		{
28
			if (isReverse)
29
				s = Reverse(s);
30
31
			var r = new List<string>();
32
33
			for (int i = 0; i < s.Length; i++)
34
			{
35
				if (i % 3 == 0 && i != 0)
36
					r.Add("_");
37
38
				r.Add(s[i].ToString());
39
			}
40
41
			if (isReverse)
42
				r.Reverse();
43
44
			return string.Join("", r);
45
		}
46
47
		double elapsed = (double)(Time.GetTicksUsec() - _time) / 1_000_000.0;
48
49
		string t = elapsed.ToString();
50
51
		if (t == "0")
52
			t += ",0";
53
54
		string[] parts = t.Split(',');
55
56
		GD.Print(
57
58
			Parse(parts[0], true)
59
			+ "."
60
			+ Parse(parts[1].PadRight(6, '0'), false)
61
			+ "\t"
62
			+ prefix
63
		);
64
	}
65
66
67
	#endregion
68
69
70
	private int once = 0;
71
	const int count = 50;
72
	const int sizeArr = 1_000_000;
73
74
	private List<int> list = [];
75
	private Godot.Collections.Array<int> godotList = [];
76
77
78
	public override void _Ready()
79
	{
80
81
	}
82
83
84
	public override void _Process(double delta)
85
	{
86
		once++;
87
88
		if (once == 120)
89
		{
90
			// Test_Each_List_Global();
91
			// Test_Each_List();
92
			// Test_Each_GodoList();
93
94
			Test_For_array();
95
			Test_For_array_new();
96
			Test_For_list();
97
			Test_For_list_AsSpan();
98
			Test_For_list_class();
99
			Test_For_list_reference();
100
			Test_For_list_reference_link();
101
			Test_For_list_reference_new();
102
			Info();
103
			ShowGc();
104
		}
105
	}
106
107
	struct Point
108
	{
109
		public int x;
110
		public int y;
111
	}
112
113
	class PointClass
114
	{
115
		public int x;
116
		public int y;
117
	}
118
119
	private void Test_For_array()
120
	{
121
		Point[] list = new Point[sizeArr];
122
		// не нужно но для чистоты
123
		for (int i = 0; i < sizeArr; i++)
124
		{
125
			list[i].x = i;
126
			list[i].y = i;
127
		}
128
129
		TimeStart();
130
		for (int a = 0; a < count; a++)
131
		{
132
			for (int i = 0; i < sizeArr; i++)
133
			{
134
				list[i].x = i + count;
135
				list[i].y = i + count;
136
			}
137
		}
138
		TimeStop("FOR array");
139
140
	}
141
142
	private void Test_For_array_new()
143
	{
144
		Point[] list = new Point[sizeArr];
145
		for (int i = 0; i < sizeArr; i++)
146
		{
147
			list[i].x = i;
148
			list[i].y = i;
149
		}
150
151
		TimeStart();
152
		for (int a = 0; a < count; a++)
153
		{
154
			for (int i = 0; i < sizeArr; i++)
155
			{
156
				Point point = list[i];
157
				point.x = i + count;
158
				point.y = i + count;
159
				list[i] = point;
160
			}
161
		}
162
		TimeStop("FOR array new");
163
164
	}
165
166
167
	private void Test_For_list()
168
	{
169
		List<Point> list = [];
170
		for (int i = 0; i < sizeArr; i++)
171
		{
172
			list.Add(new Point
173
			{
174
				x = i,
175
				y = i
176
			});
177
		}
178
179
		TimeStart();
180
		for (int a = 0; a < count; a++)
181
		{
182
			for (int i = 0; i < sizeArr; i++)
183
			{
184
				Point point = list[i];
185
				point.x = i + count;
186
				point.y = i + count;
187
				list[i] = point;
188
			}
189
		}
190
		TimeStop("FOR list");
191
	}
192
193
	private void Test_For_list_AsSpan()
194
	{
195
196
		List<Point> list = [];
197
		for (int i = 0; i < sizeArr; i++)
198
		{
199
			list.Add(new Point
200
			{
201
				x = i,
202
				y = i
203
			});
204
		}
205
		Span<Point> span = CollectionsMarshal.AsSpan(list);
206
207
		TimeStart();
208
		for (int a = 0; a < count; a++)
209
		{
210
			for (int i = 0; i < sizeArr; i++)
211
			{
212
				span[i].x = i + count;
213
				span[i].y = i + count;
214
			}
215
		}
216
		TimeStop("FOR list AsSpan");
217
	}
218
219
	private void Test_For_list_class()
220
	{
221
		List<PointClass> list = [];
222
		for (int i = 0; i < sizeArr; i++)
223
		{
224
			list.Add(new PointClass
225
			{
226
				x = i,
227
				y = i
228
			});
229
		}
230
231
		TimeStart();
232
		for (int a = 0; a < count; a++)
233
		{
234
			for (int i = 0; i < sizeArr; i++)
235
			{
236
				list[i].x = i + count;
237
				list[i].y = i + count;
238
			}
239
		}
240
		TimeStop("FOR list Class Value");
241
	}
242
243
	private class RefClass
244
	{
245
		public string x;
246
		public string y;
247
	}
248
249
	private void Test_For_list_reference()
250
	{
251
		List<RefClass> list = [];
252
		for (int i = 0; i < sizeArr; i++)
253
		{
254
			list.Add(new RefClass
255
			{
256
				x = i.ToString(),
257
				y = i.ToString()
258
			});
259
		}
260
261
		TimeStart();
262
		for (int a = 0; a < count; a++)
263
		{
264
			for (int i = 0; i < sizeArr; i++)
265
			{
266
				list[i].x = "xxxx";
267
				list[i].y = "yyyy";
268
			}
269
		}
270
		TimeStop("FOR list Class Ref literal");
271
	}
272
273
	private void Test_For_list_reference_new()
274
	{
275
		List<RefClass> list = [];
276
		for (int i = 0; i < sizeArr; i++)
277
		{
278
			list.Add(new RefClass
279
			{
280
				x = i.ToString(),
281
				y = i.ToString()
282
			});
283
		}
284
285
		TimeStart();
286
		for (int a = 0; a < count; a++)
287
		{
288
			for (int i = 0; i < sizeArr; i++)
289
			{
290
				list[i].x = (i + count).ToString();
291
				list[i].y = (i + count).ToString();
292
			}
293
		}
294
		TimeStop("FOR list Class Ref new");
295
	}
296
297
	private void Test_For_list_reference_link()
298
	{
299
		List<RefClass> list = [];
300
		var links = new string[sizeArr];
301
		for (int i = 0; i < sizeArr; i++)
302
		{
303
			list.Add(new RefClass
304
			{
305
				x = i.ToString(),
306
				y = i.ToString()
307
			});
308
			links[i] = (i + count).ToString();
309
		}
310
311
		TimeStart();
312
		for (int a = 0; a < count; a++)
313
		{
314
			for (int i = 0; i < sizeArr; i++)
315
			{
316
				list[i].x = links[i];
317
				list[i].y = links[i];
318
			}
319
		}
320
		TimeStop("FOR list Class Ref link");
321
	}
322
323
324
325
326
	void ShowGc()
327
	{
328
		GD.Print(
329
			$"{GC.GetTotalPauseDuration()}"
330
		);
331
	}
332
333
334
	private void Info()
335
	{
336
		GD.Print();
337
		GD.Print($"Кол-во итераций: {count}");
338
		GD.Print($"Размер массива:  {sizeArr}");
339
	}
340
341
	private void Test_Each_List_Global()
342
	{
343
		var globalClass = new SwapRemover();
344
		globalClass.Setup(sizeArr, count);
345
		TimeStart();
346
		globalClass.Test_Each_List();
347
		TimeStop("Each C#  List GLOBAL_CLASS\t");
348
	}
349
350
351
352
	private void Test_Each_List()
353
	{
354
		List<int> arr = [];
355
356
		for (int i = 0; i < sizeArr; i++)
357
		{
358
			arr.Add(i);
359
		}
360
361
		int r = 0;
362
		TimeStart();
363
364
		for (int i = 0; i < count; i++)
365
		{
366
			for (int a = 0; a < arr.Count; a++)
367
			{
368
				r = arr[a];
369
			}
370
		}
371
372
		TimeStop("Each C#  List\t\t\t\t");
373
	}
374
375
376
377
	private void Test_Each_GodoList()
378
	{
379
		Godot.Collections.Array<int> arr = [];
380
		// var _arr = godotList.Duplicate();
381
		int r = 0;
382
		for (int i = 0; i < sizeArr; i++)
383
		{
384
			arr.Add(i);
385
		}
386
387
		TimeStart();
388
389
		for (int i = 0; i < count; i++)
390
		{
391
			for (int a = 0; a < arr.Count; a++)
392
			{
393
				r = arr[a];
394
			}
395
		}
396
397
		TimeStop("Each C# Godot List\t\t\t");
398
	}
399
400
401
}
402