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 = 1000;
- int testIterations = 10000;
- 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();
- for (int i=0; i < testIterations; ++i)
- {
- for (int n=0; n < intArray.Length; ++n)
- {
- intArray[n] = n;
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
- }
- 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;
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
- }
- 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;
- }
- if (i==0) { stopwatch.Reset(); stopwatch.Start(); }
- }
- stopwatch.Stop();
- results.Add("Fill List<int>",stopwatch.ElapsedTicks);
- // ------------------ Results ---------------------
- string resultText = "Results: \n";
- foreach(var result in results)
- {
- resultText += result.Key + ": " + result.Value + "\n";
- }
- UnityEngine.Debug.Log(resultText);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment