Last active
December 8, 2016 19:45
-
-
Save VasylRomanets/8c4c377fb552323ef7e4b5328f9de978 to your computer and use it in GitHub Desktop.
Benchmark describes comparation of 'for' and 'foreach' in Unity 5.5.0f3 (64-bit). 1000 element arrays and lists of classes and structs were tested in loops with 500 000 iterations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Benchmark describes comparation of 'for' and 'foreach' in Unity 5.5.0f3 (64-bit). | |
| * 1000 element arrays and lists of classes and structs were tested in loops with 500 000 iterations. | |
| */ | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using UnityEngine; | |
| namespace Benchmarks | |
| { | |
| public class ForVSForeachBenchmark : MonoBehaviour | |
| { | |
| private const int iterations = 500000; | |
| private const int collectionSize = 1000; | |
| private IEnumerator Start() | |
| { | |
| UnityEngine.Debug.Log("5 second delay"); | |
| yield return new WaitForSeconds(5f); | |
| object[] arrayClass = CreateArray<object>(); | |
| List<object> listClass = CreateList<object>(); | |
| long forArrayClassResult = ForArray(arrayClass); | |
| long forListClassResult = ForList(listClass); | |
| long foreachArrayClassResult = ForeachArray(arrayClass); | |
| long foreachListClassResult = ForeachList(listClass); | |
| int[] arrayStruct = CreateArray<int>(); | |
| List<int> listStruct = CreateList<int>(); | |
| long forArrayStructResult = ForArray(arrayClass); | |
| long forListStructResult = ForList(listClass); | |
| long foreachArrayStructResult = ForeachArray(arrayClass); | |
| long foreachListStructResult = ForeachList(listClass); | |
| UnityEngine.Debug.Log("Array of classes: " + "For = " + forArrayClassResult + " ms, Foreach = " + foreachArrayClassResult + " ms"); | |
| UnityEngine.Debug.Log("Array of structs: " + "For = " + forArrayStructResult + " ms, Foreach = " + foreachArrayStructResult + " ms"); | |
| UnityEngine.Debug.Log("List of classes: " + "For = " + forListClassResult + " ms, Foreach = " + foreachListClassResult + " ms"); | |
| UnityEngine.Debug.Log("List of structs: " + "For = " + forListStructResult + " ms, Foreach = " + foreachListStructResult + " ms"); | |
| } | |
| private T[] CreateArray<T>() where T : new() | |
| { | |
| var array = new T[collectionSize]; | |
| for(int i = 0; i < collectionSize; i++) | |
| array[i] = new T(); | |
| return array; | |
| } | |
| private List<T> CreateList<T>() where T : new() | |
| { | |
| var list = new List<T>(collectionSize); | |
| for(int i = 0; i < collectionSize; i++) | |
| list.Add(new T()); | |
| return list; | |
| } | |
| private long ForArray<T>(T[] array) | |
| { | |
| var timer = Stopwatch.StartNew(); | |
| T temp; | |
| for(int i = 0; i < iterations; i++) | |
| { | |
| for(int k = 0; k < array.Length; k++) | |
| { | |
| temp = array[k]; | |
| } | |
| } | |
| timer.Stop(); | |
| return timer.ElapsedMilliseconds; | |
| } | |
| private long ForList<T>(List<T> list) | |
| { | |
| var timer = Stopwatch.StartNew(); | |
| T temp; | |
| for(int i = 0; i < iterations; i++) | |
| { | |
| for(int k = 0; k < list.Count; k++) | |
| { | |
| temp = list[k]; | |
| } | |
| } | |
| timer.Stop(); | |
| return timer.ElapsedMilliseconds; | |
| } | |
| private long ForeachArray<T>(T[] array) | |
| { | |
| var timer = Stopwatch.StartNew(); | |
| T temp; | |
| for(int i = 0; i < iterations; i++) | |
| { | |
| foreach(var item in array) | |
| { | |
| temp = item; | |
| } | |
| } | |
| timer.Stop(); | |
| return timer.ElapsedMilliseconds; | |
| } | |
| private long ForeachList<T>(List<T> list) | |
| { | |
| var timer = Stopwatch.StartNew(); | |
| T temp; | |
| for(int i = 0; i < iterations; i++) | |
| { | |
| foreach(var item in list) | |
| { | |
| temp = item; | |
| } | |
| } | |
| timer.Stop(); | |
| return timer.ElapsedMilliseconds; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment