Guest User

Untitled

a guest
Sep 9th, 2026
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  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_new();
  101.             Info();
  102.             ShowGc();
  103.         }
  104.     }
  105.  
  106.     struct Point
  107.     {
  108.         public int x;
  109.         public int y;
  110.     }
  111.  
  112.     class PointClass
  113.     {
  114.         public int x;
  115.         public int y;
  116.     }
  117.  
  118.     private void Test_For_array()
  119.     {
  120.         Point[] list = new Point[sizeArr];
  121.         // не нужно но для чистоты
  122.         for (int i = 0; i < sizeArr; i++)
  123.         {
  124.             list[i].x = i;
  125.             list[i].y = i;
  126.         }
  127.  
  128.         TimeStart();
  129.         for (int a = 0; a < count; a++)
  130.         {
  131.             for (int i = 0; i < sizeArr; i++)
  132.             {
  133.                 list[i].x = i + count;
  134.                 list[i].y = i + count;
  135.             }
  136.         }
  137.         TimeStop("FOR array");
  138.  
  139.     }
  140.  
  141.     private void Test_For_array_new()
  142.     {
  143.         Point[] list = new Point[sizeArr];
  144.         for (int i = 0; i < sizeArr; i++)
  145.         {
  146.             list[i].x = i;
  147.             list[i].y = i;
  148.         }
  149.  
  150.         TimeStart();
  151.         for (int a = 0; a < count; a++)
  152.         {
  153.             for (int i = 0; i < sizeArr; i++)
  154.             {
  155.                 Point point = list[i];
  156.                 point.x = i + count;
  157.                 point.y = i + count;
  158.                 list[i] = point;
  159.             }
  160.         }
  161.         TimeStop("FOR array new");
  162.  
  163.     }
  164.  
  165.  
  166.     private void Test_For_list()
  167.     {
  168.         List<Point> list = [];
  169.         for (int i = 0; i < sizeArr; i++)
  170.         {
  171.             list.Add(new Point
  172.             {
  173.                 x = i,
  174.                 y = i
  175.             });
  176.         }
  177.  
  178.         TimeStart();
  179.         for (int a = 0; a < count; a++)
  180.         {
  181.             for (int i = 0; i < sizeArr; i++)
  182.             {
  183.                 Point point = list[i];
  184.                 point.x = i + count;
  185.                 point.y = i + count;
  186.                 list[i] = point;
  187.             }
  188.         }
  189.         TimeStop("FOR list");
  190.     }
  191.  
  192.     private void Test_For_list_AsSpan()
  193.     {
  194.  
  195.         List<Point> list = [];
  196.         for (int i = 0; i < sizeArr; i++)
  197.         {
  198.             list.Add(new Point
  199.             {
  200.                 x = i,
  201.                 y = i
  202.             });
  203.         }
  204.         Span<Point> span = CollectionsMarshal.AsSpan(list);
  205.  
  206.         TimeStart();
  207.         for (int a = 0; a < count; a++)
  208.         {
  209.             for (int i = 0; i < sizeArr; i++)
  210.             {
  211.                 span[i].x = i + count;
  212.                 span[i].y = i + count;
  213.             }
  214.         }
  215.         TimeStop("FOR list AsSpan");
  216.     }
  217.  
  218.     private void Test_For_list_class()
  219.     {
  220.         List<PointClass> list = [];
  221.         for (int i = 0; i < sizeArr; i++)
  222.         {
  223.             list.Add(new PointClass
  224.             {
  225.                 x = i,
  226.                 y = i
  227.             });
  228.         }
  229.  
  230.         TimeStart();
  231.         for (int a = 0; a < count; a++)
  232.         {
  233.             for (int i = 0; i < sizeArr; i++)
  234.             {
  235.                 list[i].x = i + count;
  236.                 list[i].y = i + count;
  237.             }
  238.         }
  239.         TimeStop("FOR list Class Value");
  240.     }
  241.  
  242.     private class RefClass
  243.     {
  244.         public string x;
  245.         public string y;
  246.     }
  247.  
  248.     private void Test_For_list_reference()
  249.     {
  250.         List<RefClass> list = [];
  251.         for (int i = 0; i < sizeArr; i++)
  252.         {
  253.             list.Add(new RefClass
  254.             {
  255.                 x = i.ToString(),
  256.                 y = i.ToString()
  257.             });
  258.         }
  259.  
  260.         TimeStart();
  261.         for (int a = 0; a < count; a++)
  262.         {
  263.             for (int i = 0; i < sizeArr; i++)
  264.             {
  265.                 list[i].x = "xxxx";
  266.                 list[i].y = "yyyy";
  267.             }
  268.         }
  269.         TimeStop("FOR list Class Ref literal");
  270.     }
  271.  
  272.     private void Test_For_list_reference_new()
  273.     {
  274.         List<RefClass> list = [];
  275.         for (int i = 0; i < sizeArr; i++)
  276.         {
  277.             list.Add(new RefClass
  278.             {
  279.                 x = i.ToString(),
  280.                 y = i.ToString()
  281.             });
  282.         }
  283.  
  284.         TimeStart();
  285.         for (int a = 0; a < count; a++)
  286.         {
  287.             for (int i = 0; i < sizeArr; i++)
  288.             {
  289.                 list[i].x = (i + count).ToString();
  290.                 list[i].y = (i + count).ToString();
  291.             }
  292.         }
  293.         TimeStop("FOR list Class Ref new");
  294.     }
  295.  
  296.  
  297.  
  298.     void ShowGc()
  299.     {
  300.         GD.Print(
  301.             $"{GC.GetTotalPauseDuration()}"
  302.         );
  303.     }
  304.  
  305.  
  306.     private void Info()
  307.     {
  308.         GD.Print();
  309.         GD.Print($"Кол-во итераций: {count}");
  310.         GD.Print($"Размер массива:  {sizeArr}");
  311.     }
  312.  
  313.     private void Test_Each_List_Global()
  314.     {
  315.         var globalClass = new SwapRemover();
  316.         globalClass.Setup(sizeArr, count);
  317.         TimeStart();
  318.         globalClass.Test_Each_List();
  319.         TimeStop("Each C#  List GLOBAL_CLASS\t");
  320.     }
  321.  
  322.  
  323.  
  324.     private void Test_Each_List()
  325.     {
  326.         List<int> arr = [];
  327.  
  328.         for (int i = 0; i < sizeArr; i++)
  329.         {
  330.             arr.Add(i);
  331.         }
  332.  
  333.         int r = 0;
  334.         TimeStart();
  335.  
  336.         for (int i = 0; i < count; i++)
  337.         {
  338.             for (int a = 0; a < arr.Count; a++)
  339.             {
  340.                 r = arr[a];
  341.             }
  342.         }
  343.  
  344.         TimeStop("Each C#  List\t\t\t\t");
  345.     }
  346.  
  347.  
  348.  
  349.     private void Test_Each_GodoList()
  350.     {
  351.         Godot.Collections.Array<int> arr = [];
  352.         // var _arr = godotList.Duplicate();
  353.         int r = 0;
  354.         for (int i = 0; i < sizeArr; i++)
  355.         {
  356.             arr.Add(i);
  357.         }
  358.  
  359.         TimeStart();
  360.  
  361.         for (int i = 0; i < count; i++)
  362.         {
  363.             for (int a = 0; a < arr.Count; a++)
  364.             {
  365.                 r = arr[a];
  366.             }
  367.         }
  368.  
  369.         TimeStop("Each C# Godot List\t\t\t");
  370.     }
  371.  
  372.  
  373. }
  374.  
Advertisement
Add Comment
Please, Sign In to add comment