duck

Untitled

Jul 6th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.Collections;
  7. using System.Diagnostics;
  8.  
  9. namespace SpeedTest
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.  
  16.             Stopwatch stopwatch = new Stopwatch();
  17.             int testLength = 10000;
  18.             int testIterations = 5000;
  19.             var results = new Dictionary<string, float>();
  20.  
  21.             // initialise the collections. pre-filling with zeros is not part of the test (but takes a long time)
  22.             int[] intArray = new int[testLength];
  23.  
  24.             ArrayList intArrayList = new ArrayList(testLength);
  25.             for (int n = 0; n < testLength; ++n) { intArrayList.Add(0); }
  26.  
  27.             List<int> intList = new List<int>(testLength);
  28.             for (int n = 0; n < testLength; ++n) { intList.Add(0); }
  29.  
  30.  
  31.             int a = 0;
  32.  
  33.             // ----------------- int[] ----------------------
  34.  
  35.             stopwatch.Reset();
  36.             stopwatch.Start();
  37.  
  38.  
  39.             for (int i = 0; i < testIterations; ++i)
  40.             {
  41.                 for (int n = 0; n < intArray.Length; ++n)
  42.                 {
  43.                     intArray[n] = n;
  44.                     intArray[n]++;
  45.                     a = intArray[n];
  46.                 }
  47.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  48.             }
  49.             stopwatch.Stop();
  50.             Console.WriteLine("done " + intArray.Length + " on intArray");
  51.             results.Add("Direct Length int[]", stopwatch.ElapsedTicks);
  52.  
  53.  
  54.  
  55.             // ----------------- List<int> ----------------------
  56.  
  57.             stopwatch.Reset();
  58.             stopwatch.Start();
  59.             for (int i = 0; i < testIterations; ++i)
  60.             {
  61.                 for (int n = 0; n < intList.Count; ++n)
  62.                 {
  63.                     intList[n] = n;
  64.                     intList[n]++;
  65.                     a = intList[n];
  66.                 }
  67.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  68.             }
  69.             stopwatch.Stop();
  70.             Console.WriteLine("done " + intList.Count + " on intList");
  71.             results.Add("Direct Length List<int>", stopwatch.ElapsedTicks);
  72.  
  73.  
  74.             // ----------------- ArrayList ----------------------
  75.  
  76.             stopwatch.Reset();
  77.             stopwatch.Start();
  78.             for (int i = 0; i < testIterations; ++i)
  79.             {
  80.                 for (int n = 0; n < intArrayList.Count; ++n)
  81.                 {
  82.                     intArrayList[n] = n;
  83.                     intArrayList[n] = ((int)intArrayList[n]) + 1;
  84.                     a = (int)intArrayList[n];
  85.                 }
  86.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  87.             }
  88.             stopwatch.Stop();
  89.             Console.WriteLine("done " + intArrayList.Count + " on intArrayList");
  90.             results.Add("Direct Length ArrayList", stopwatch.ElapsedTicks);
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.             // ----------------- and now, without bounds check optimisation
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.             // ----------------- int[] ----------------------
  109.  
  110.             stopwatch.Reset();
  111.             stopwatch.Start();
  112.  
  113.  
  114.             for (int i = 0; i < testIterations; ++i)
  115.             {
  116.                 for (int n = 0; n < testLength; ++n)
  117.                 {
  118.                     intArray[n] = n;
  119.                     intArray[n]++;
  120.                     a = intArray[n];
  121.                 }
  122.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  123.             }
  124.             stopwatch.Stop();
  125.             Console.WriteLine("done " + intArray.Length + " on intArray");
  126.             results.Add("Cached Length int[]", stopwatch.ElapsedTicks);
  127.  
  128.             // ----------------- List<int> ----------------------
  129.  
  130.             stopwatch.Reset();
  131.             stopwatch.Start();
  132.             for (int i = 0; i < testIterations; ++i)
  133.             {
  134.                 for (int n = 0; n < testLength; ++n)
  135.                 {
  136.                     intList[n] = n;
  137.                     intList[n]++;
  138.                     a = intList[n];
  139.                 }
  140.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  141.             }
  142.             stopwatch.Stop();
  143.             Console.WriteLine("done " + intList.Count + " on intList");
  144.             results.Add("Cached Length List<int>", stopwatch.ElapsedTicks);
  145.  
  146.  
  147.             // ----------------- ArrayList ----------------------
  148.  
  149.             stopwatch.Reset();
  150.             stopwatch.Start();
  151.             for (int i = 0; i < testIterations; ++i)
  152.             {
  153.                 for (int n = 0; n < testLength; ++n)
  154.                 {
  155.                     intArrayList[n] = n;
  156.                     intArrayList[n] = ((int)intArrayList[n]) + 1;
  157.                     a = (int)intArrayList[n];
  158.                 }
  159.                 if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  160.             }
  161.             stopwatch.Stop();
  162.             Console.WriteLine("done " + intArrayList.Count + " on intArrayList");
  163.             results.Add("Cached Length ArrayList", stopwatch.ElapsedTicks);
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.             // ------------------ Results ---------------------
  171.  
  172.             string resultText = "Results: \n";
  173.             foreach (var result in results)
  174.             {
  175.                 resultText += result.Key + ": " + ((result.Value / System.TimeSpan.TicksPerSecond) * 1000).ToString("0") + "ms \n";
  176.             }
  177.  
  178.             Console.WriteLine(resultText);
  179.  
  180.             // prevent compiler from knowing 'a' is never used!
  181.             Console.WriteLine(a);
  182.  
  183.  
  184.             Console.ReadLine();
  185.  
  186.  
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment