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 = 10000;
- var results = new Dictionary<string, float>();
- // initialise the collections. pre-filling with zeros is not part of the test (but takes a long time)
- int[] intArray = new int[testLength];
- ArrayList intArrayList = new ArrayList(testLength);
- for (int n=0; n< testLength; ++n) { intArrayList.Add(0); }
- List<int> intList = new List<int>(testLength);
- for (int n=0; n< testLength; ++n) { intList.Add(0); }
- int a = 0;
- // ----------------- 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();
- UnityEngine.Debug.Log("done "+intArrayList.Count+" on intArrayList");
- results.Add("ArrayList",stopwatch.ElapsedTicks);
- // ----------------- int[] ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- 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();
- UnityEngine.Debug.Log("done "+intArray.Length+" on intArray");
- results.Add("int[]",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();
- UnityEngine.Debug.Log("done "+intList.Count+" on intList");
- results.Add("List<int>",stopwatch.ElapsedTicks);
- // ----------------- and now, without bounds check optimisation
- // ----------------- ArrayList ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < testLength; ++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();
- UnityEngine.Debug.Log("done "+intArrayList.Count+" on intArrayList");
- results.Add("NBCO ArrayList",stopwatch.ElapsedTicks);
- // ----------------- int[] ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < testLength; ++n)
- {
- intArray[n] = n;
- intArray[n]++;
- a = intArray[n];
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- UnityEngine.Debug.Log("done "+intArray.Length+" on intArray");
- results.Add("NBCO int[]",stopwatch.ElapsedTicks);
- // ----------------- List<int> ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < testLength; ++n)
- {
- intList[n] = n;
- intList[n]++;
- a = intList[n];
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- UnityEngine.Debug.Log("done "+intList.Count+" on intList");
- results.Add("NBCO 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