Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace test
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random random = new Random();
- List<int> listTest = new List<int>();
- for (int i = 0; i < 500000; i++)
- {
- listTest.Add(random.Next(1001));
- }
- Stopwatch sw2 = new Stopwatch();
- sw2.Start();
- Method1(listTest);
- sw2.Stop();
- Console.WriteLine("Elapsed={0}", sw2.Elapsed);
- Stopwatch sw = new Stopwatch();
- sw.Start();
- Method2(ref listTest);
- sw.Stop();
- Console.WriteLine("ref - Elapsed={0}", sw.Elapsed);
- Console.ReadLine();
- }
- static int Method1(List<int> listTest)
- {
- return listTest[listTest.Count / 2];
- }
- static int Method2(ref List<int> listTest)
- {
- return listTest[listTest.Count / 2];
- }
- /*
- [+] -> faster
- Results:
- ref - Elapsed=00:00:00.0001414 +
- Elapsed=00:00:00.0002133
- ref - Elapsed=00:00:00.0001943 +
- Elapsed=00:00:00.0004715
- ref - Elapsed=00:00:00.0001965 +
- Elapsed=00:00:00.0002490
- ref - Elapsed=00:00:00.0002220
- Elapsed=00:00:00.0001969 +
- ref - Elapsed=00:00:00.0002064
- Elapsed=00:00:00.0001320 +
- ref - Elapsed=00:00:00.0001630
- Elapsed=00:00:00.0001509 +
- ref - Elapsed=00:00:00.0002603
- Elapsed=00:00:00.0001659 +
- ref - Elapsed=00:00:00.0001739
- Elapsed=00:00:00.0001403 +
- Elapsed=00:00:00.0002337
- ref - Elapsed=00:00:00.0001436 +
- Elapsed=00:00:00.0001655
- ref - Elapsed=00:00:00.0001382 +
- Elapsed=00:00:00.0001739 +
- ref - Elapsed=00:00:00.0025709
- Elapsed=00:00:00.0003128
- ref - Elapsed=00:00:00.0001524 +
- Elapsed=00:00:00.0001659
- ref - Elapsed=00:00:00.0001487 +
- */
- }
- }
Advertisement