duck

Untitled

Jul 6th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 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 = 100000;
  13.         var results = new Dictionary<string, float>();
  14.        
  15.        
  16.         int[] intArray = new int[testLength];
  17.         ArrayList intArrayList = new ArrayList(testLength);
  18.         List<int> intList = new List<int>(testLength);
  19.        
  20.        
  21.         // ----------------- int[] ----------------------
  22.        
  23.         stopwatch.Reset();
  24.         stopwatch.Start();
  25.        
  26.         int a = 0;
  27.        
  28.         for (int i=0; i < testIterations; ++i)
  29.         {
  30.             for (int n=0; n < intArray.Length; ++n)
  31.             {
  32.                 intArray[n] = n;
  33.                 intArray[n]++;
  34.                 a = intArray[n];
  35.             }
  36.             if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  37.         }
  38.         stopwatch.Stop();
  39.         results.Add("Fill int[]",stopwatch.ElapsedTicks);
  40.        
  41.        
  42.         // ----------------- ArrayList ----------------------
  43.        
  44.         stopwatch.Reset();
  45.         stopwatch.Start();
  46.         for (int i=0; i < testIterations; ++i)
  47.         {
  48.             for (int n=0; n < intArrayList.Count; ++n)
  49.             {
  50.                 intArrayList[n] = n;
  51.                 intArrayList[n] = ((int)intArrayList[n])+1;
  52.                 a = (int)intArrayList[n];
  53.             }
  54.             if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  55.         }
  56.         stopwatch.Stop();
  57.         results.Add("Fill ArrayList",stopwatch.ElapsedTicks);
  58.        
  59.        
  60.        
  61.         // ----------------- List<int> ----------------------
  62.        
  63.         stopwatch.Reset();
  64.         stopwatch.Start();
  65.         for (int i=0; i < testIterations; ++i)
  66.         {
  67.             for (int n=0; n < intList.Count; ++n)
  68.             {
  69.                 intList[n] = n;
  70.                 intList[n]++;
  71.                 a = intList[n];
  72.             }
  73.             if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
  74.         }
  75.         stopwatch.Stop();
  76.         results.Add("Fill List<int>",stopwatch.ElapsedTicks);
  77.        
  78.        
  79.        
  80.        
  81.         // ------------------ Results ---------------------
  82.        
  83.         string resultText = "Results: \n";
  84.         foreach(var result in results)
  85.         {
  86.             resultText += result.Key + ": " + ((result.Value / System.TimeSpan.TicksPerSecond)*1000).ToString("####0.00") + "ms \n";
  87.         }
  88.        
  89.         UnityEngine.Debug.Log(resultText);
  90.        
  91.         // prevent compiler from knowing 'a' is never used!
  92.         UnityEngine.Debug.Log(a);
  93.        
  94.     }
  95.    
  96. }
Advertisement
Add Comment
Please, Sign In to add comment