duck

Untitled

Jul 6th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 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 = 1000;
  12.     int testIterations = 10000;
  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.     for (int i=0; i < testIterations; ++i)
  26.     {
  27.       for (int n=0; n < intArray.Length; ++n)
  28.       {
  29.         intArray[n] = n;
  30.       }
  31.       if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
  32.     }
  33.     stopwatch.Stop();
  34.     results.Add("Fill int[]",stopwatch.ElapsedTicks);
  35.    
  36.    
  37.     // ----------------- ArrayList ----------------------
  38.    
  39.     stopwatch.Reset();
  40.     stopwatch.Start();
  41.     for (int i=0; i < testIterations; ++i)
  42.     {
  43.       for (int n=0; n < intArrayList.Count; ++n)
  44.       {
  45.         intArrayList[n] = n;
  46.       }
  47.       if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
  48.     }
  49.     stopwatch.Stop();
  50.     results.Add("Fill ArrayList",stopwatch.ElapsedTicks);
  51.    
  52.    
  53.    
  54.     // ----------------- List<int> ----------------------
  55.    
  56.     stopwatch.Reset();
  57.     stopwatch.Start();
  58.     for (int i=0; i < testIterations; ++i)
  59.     {
  60.       for (int n=0; n < intList.Count; ++n)
  61.       {
  62.         intList[n] = n;
  63.       }
  64.       if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
  65.     }
  66.     stopwatch.Stop();
  67.     results.Add("Fill List<int>",stopwatch.ElapsedTicks);
  68.    
  69.    
  70.    
  71.    
  72.     // ------------------ Results ---------------------
  73.    
  74.     string resultText = "Results: \n";
  75.     foreach(var result in results)
  76.     {
  77.       resultText += result.Key + ": " + result.Value + "\n";
  78.     }
  79.    
  80.     UnityEngine.Debug.Log(resultText);
  81.    
  82.    
  83.    
  84.   }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment