Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- public class SpeedTest : MonoBehaviour {
- void Start () {
- Stopwatch stopwatch = new Stopwatch();
- int testLength = 10000;
- int testIterations = 100000;
- var results = new Dictionary<string, float>();
- int[] intArray = new int[testLength];
- ArrayList intArrayList = new ArrayList(testLength);
- List<int> intList = new List<int>(testLength);
- // ----------------- int[] ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- int a = 0;
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < intArray.Length; ++n)
- {
- intArray[n] = n;
- intArray[n]++;
- a = intArray[n];
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- results.Add("Fill int[]",stopwatch.ElapsedTicks);
- // ----------------- ArrayList ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < intArrayList.Count; ++n)
- {
- intArrayList[n] = n;
- intArrayList[n] = ((int)intArrayList[n])+1;
- a = (int)intArrayList[n];
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- results.Add("Fill ArrayList",stopwatch.ElapsedTicks);
- // ----------------- List<int> ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < intList.Count; ++n)
- {
- intList[n] = n;
- intList[n]++;
- a = intList[n];
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- results.Add("Fill List<int>",stopwatch.ElapsedTicks);
- // ------------------ Results ---------------------
- string resultText = "Results: \n";
- foreach(var result in results)
- {
- resultText += result.Key + ": " + ((result.Value / System.TimeSpan.TicksPerSecond)*1000).ToString("####0.00") + "ms \n";
- }
- UnityEngine.Debug.Log(resultText);
- // prevent compiler from knowing 'a' is never used!
- UnityEngine.Debug.Log(a);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment