duck

Untitled

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