Hyluss

Passing list<int> by ref. Speed test

Jun 7th, 2019
321
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace test
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Random random = new Random();
  15.  
  16.             List<int> listTest = new List<int>();
  17.  
  18.             for (int i = 0; i < 500000; i++)
  19.             {
  20.                 listTest.Add(random.Next(1001));
  21.             }
  22.  
  23.             Stopwatch sw2 = new Stopwatch();
  24.             sw2.Start();
  25.             Method1(listTest);
  26.             sw2.Stop();
  27.             Console.WriteLine("Elapsed={0}", sw2.Elapsed);
  28.  
  29.  
  30.             Stopwatch sw = new Stopwatch();
  31.             sw.Start();
  32.             Method2(ref listTest);
  33.             sw.Stop();
  34.             Console.WriteLine("ref - Elapsed={0}", sw.Elapsed);
  35.  
  36.             Console.ReadLine();
  37.         }
  38.  
  39.         static int Method1(List<int> listTest)
  40.         {
  41.             return listTest[listTest.Count / 2];
  42.         }
  43.  
  44.         static int Method2(ref List<int> listTest)
  45.         {
  46.             return listTest[listTest.Count / 2];
  47.         }
  48.  
  49.         /*            
  50.            [+] -> faster
  51.  
  52.            Results:
  53.  
  54.            ref - Elapsed=00:00:00.0001414 +
  55.            Elapsed=00:00:00.0002133
  56.            
  57.            ref - Elapsed=00:00:00.0001943 +
  58.            Elapsed=00:00:00.0004715
  59.            
  60.            ref - Elapsed=00:00:00.0001965 +
  61.            Elapsed=00:00:00.0002490
  62.            
  63.            ref - Elapsed=00:00:00.0002220
  64.            Elapsed=00:00:00.0001969 +
  65.            
  66.            ref - Elapsed=00:00:00.0002064
  67.            Elapsed=00:00:00.0001320 +
  68.            
  69.            ref - Elapsed=00:00:00.0001630
  70.            Elapsed=00:00:00.0001509 +
  71.            
  72.            ref - Elapsed=00:00:00.0002603
  73.            Elapsed=00:00:00.0001659 +
  74.            
  75.            ref - Elapsed=00:00:00.0001739
  76.            Elapsed=00:00:00.0001403 +
  77.            
  78.            Elapsed=00:00:00.0002337
  79.            ref - Elapsed=00:00:00.0001436 +
  80.            
  81.            Elapsed=00:00:00.0001655
  82.            ref - Elapsed=00:00:00.0001382 +
  83.            
  84.            Elapsed=00:00:00.0001739 +
  85.            ref - Elapsed=00:00:00.0025709
  86.            
  87.            Elapsed=00:00:00.0003128
  88.            ref - Elapsed=00:00:00.0001524 +
  89.            
  90.            Elapsed=00:00:00.0001659
  91.            ref - Elapsed=00:00:00.0001487 +
  92.  
  93.          */
  94.  
  95.     }
  96. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment