Skip to content

Instantly share code, notes, and snippets.

@vgrudenic
Created May 15, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrudenic/c0617744748253be20260aacfe22aaf5 to your computer and use it in GitHub Desktop.
Save vgrudenic/c0617744748253be20260aacfe22aaf5 to your computer and use it in GitHub Desktop.
Comparing struct/class access with shuffled arrays
using System;
using UnityEngine;
class TestScript : MonoBehaviour
{
struct ProjectileStruct
{
public Vector3 Position;
public Vector3 Velocity;
}
class ProjectileClass
{
public Vector3 Position;
public Vector3 Velocity;
}
void Start()
{
const int count = 10000000;
ProjectileStruct[] projectileStructs = new ProjectileStruct[count];
ProjectileClass[] projectileClasses = new ProjectileClass[count];
for (int i = 0; i < count; ++i)
{
projectileClasses[i] = new ProjectileClass();
}
Shuffle(projectileStructs);
Shuffle(projectileClasses);
// create an array of shuffled indices
int[] indices = new int[count];
for (int i = 0; i < count; i++)
{
indices[i] = i;
}
Shuffle(indices);
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < count; ++i)
{
UpdateProjectile(ref projectileStructs[indices[i]], 0.5f);
}
long structTime = sw.ElapsedMilliseconds;
sw.Reset();
sw.Start();
for (int i = 0; i < count; ++i)
{
UpdateProjectile(projectileClasses[indices[i]], 0.5f);
}
long classTime = sw.ElapsedMilliseconds;
string report = string.Format(
"Type,Time\n" +
"Struct,{0}\n" +
"Class,{1}\n",
structTime,
classTime
);
Debug.Log(report);
}
void UpdateProjectile(ref ProjectileStruct projectile, float time)
{
projectile.Position += projectile.Velocity * time;
}
void UpdateProjectile(ProjectileClass projectile, float time)
{
projectile.Position += projectile.Velocity * time;
}
public static void Shuffle<T>(T[] list)
{
System.Random random = new System.Random();
for (int n = list.Length; n > 1; )
{
n--;
int k = random.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment