Created
June 9, 2018 09:11
-
-
Save 5argon/252466c721e89cdaf8aec6a429afceeb to your computer and use it in GitHub Desktop.
Comparing read-write speed of Unity's ECS native container against regular arrays. Read-write linearly.
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
using UnityEngine; | |
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Collections; | |
using Unity.Jobs; | |
using UnityEngine.Experimental.PlayerLoop; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public class Sandbox : JobComponentSystem | |
{ | |
NativeArray<int> nativeArray; | |
int[] array; | |
private const int amount = 100000; | |
Stopwatch sw; | |
protected override void OnCreateManager(int capacity) | |
{ | |
nativeArray = new NativeArray<int>(amount, Allocator.Persistent); | |
array = new int[amount]; | |
// for (int i = 0; i < amount; i++) | |
// { | |
// int random = UnityEngine.Random.Range(0,100); | |
// nativeArray[i] = random; | |
// array[i] = random; | |
// } | |
sw = new Stopwatch(); | |
} | |
protected override void OnDestroyManager() | |
{ | |
nativeArray.Dispose(); | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
int random = UnityEngine.Random.Range(0,100); | |
int read = 0; | |
WarmUp(array); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
read = array[i]; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(array)} R {sw.ElapsedTicks}"); | |
sw.Reset(); | |
WarmUp(nativeArray); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
read = nativeArray[i]; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(nativeArray)} R {sw.ElapsedTicks}"); | |
sw.Reset(); | |
WarmUp(array); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
array[i] = random; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(array)} W {sw.ElapsedTicks}"); | |
sw.Reset(); | |
WarmUp(nativeArray); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
nativeArray[i] = random; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(nativeArray)} W {sw.ElapsedTicks}"); | |
sw.Reset(); | |
WarmUp(array); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
array[i]++; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(array)} RW {sw.ElapsedTicks}"); | |
sw.Reset(); | |
WarmUp(nativeArray); | |
sw.Start(); | |
for (int i = 0; i < amount; i++) | |
{ | |
nativeArray[i]++; | |
} | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(nativeArray)} RW {sw.ElapsedTicks}"); | |
sw.Reset(); | |
var rwJob = new RWJob() { nativeArray = nativeArray }; | |
rwJob.Schedule().Complete(); //Warm up | |
sw.Start(); | |
rwJob.Schedule().Complete(); | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(nativeArray)} RW (Job) {sw.ElapsedTicks}"); | |
sw.Reset(); | |
int[] sizes = new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256 }; | |
foreach (int size in sizes) | |
{ | |
var rwJobParallel = new RWJobParallel() { nativeArray = nativeArray }; | |
rwJobParallel.Schedule(amount, size).Complete(); //Warm up | |
sw.Start(); | |
rwJobParallel.Schedule(amount, size).Complete(); | |
sw.Stop(); | |
UnityEngine.Debug.Log($"{nameof(nativeArray)} RW (Job Parallel {size}) {sw.ElapsedTicks}"); | |
sw.Reset(); | |
} | |
this.Enabled = false; | |
return inputDeps; | |
} | |
struct RWJob : IJob | |
{ | |
public NativeArray<int> nativeArray; | |
public void Execute() | |
{ | |
for (int i = 0; i < amount; i++) | |
{ | |
nativeArray[i]++; | |
} | |
} | |
} | |
struct RWJobParallel : IJobParallelFor | |
{ | |
public NativeArray<int> nativeArray; | |
public void Execute(int index) | |
{ | |
nativeArray[index]++; | |
} | |
} | |
private static void WarmUp(int[] a) | |
{ | |
int random = UnityEngine.Random.Range(0, 100); | |
for (int i = 0; i < a.Length; i++) | |
{ | |
a[i] = random; | |
} | |
} | |
private static void WarmUp(NativeArray<int> a) | |
{ | |
int random = UnityEngine.Random.Range(0, 100); | |
for (int i = 0; i < a.Length; i++) | |
{ | |
a[i] = random; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment