Skip to content

Instantly share code, notes, and snippets.

@Arakade
Last active March 31, 2017 21:11
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 Arakade/d27f615c023a226f02f7f8ae1aec2e0c to your computer and use it in GitHub Desktop.
Save Arakade/d27f615c023a226f02f7f8ae1aec2e0c to your computer and use it in GitHub Desktop.
Unity3D / C# coder? Can you guess the answers? What's your score?
using System;
using UnityEngine;
namespace UGS {
class SizeTesting : MonoBehaviour {
private const string msgFmt = "{0}:\t\t{1:0.000} bytes";
public void Awake() {
v<byte>();
v<int>();
v<float>();
v<Struct01>();
v<StructNoPos>();
v<StructByteIso>();
v<StructByte1>();
v<StructByte2>();
v<StructByte3>();
v<StructByte4>();
v<StructByte5>();
v<StructInt1>();
v<StructInt2>();
v<StructInt3>();
v<StructInt4>();
v<StructInt5>();
v<StructInt6>();
v<StructFloat1>();
v<StructFloat2>();
v<StructFloat3>();
v<StructFloat4>();
v<StructFloat5>();
v<StructFloat6>();
r<ClassByte1>();
r<ClassFloat1>();
r<ClassFloat2>();
r<ClassFloat3>();
r<ClassFloat4>();
r<ClassFloat5>();
r<SealedClass>();
r<SealedClassNoPos>();
r<SealedClassByteIso>();
}
private static void r<T>() where T : class, new() {
Debug.LogFormat(msgFmt, typeof(T).Name, InstanceOverheadTestReferenceType<T>());
}
private static void v<T>() where T : struct {
Debug.LogFormat(msgFmt, typeof(T).Name, InstanceOverheadTestValueType<T>());
}
// using code from http://stackoverflow.com/questions/22986431/how-much-memory-instance-of-my-class-uses-pragmatic-answer
const int Size = 100000;
private static double InstanceOverheadTestReferenceType<T>() where T : class, new() {
object[] array = new object[Size];
long initialMemory = GC.GetTotalMemory(true);
for (int i = 0; i < Size; i++) {
array[i] = new T();
}
long finalMemory = GC.GetTotalMemory(true);
GC.KeepAlive(array);
long total = finalMemory - initialMemory;
return total / (double) Size;
}
private static double InstanceOverheadTestValueType<T>() where T : struct {
long initialMemory = GC.GetTotalMemory(true);
T[] array = new T[Size];
// value type shouldn't need initiailizing
//for (int i = 0; i < Size; i++) {
// array[i] = new T();
//}
long finalMemory = GC.GetTotalMemory(true);
GC.KeepAlive(array);
long total = finalMemory - initialMemory;
return total / (double) Size;
}
}
#pragma warning disable 0169
// 40 bytes
sealed class SealedClass {
private float isovalue;
private TerraVol.Vector3i position;
private byte flags;
}
// 23 bytes
sealed class SealedClassNoPos {
private float isovalue;
private byte flags;
}
// 23 bytes
sealed class SealedClassByteIso {
private byte isovalue;
private byte flags;
}
struct Struct01 {
private float isovalue;
private TerraVol.Vector3i position;
private byte flags;
}
struct StructNoPos {
private float isovalue;
private byte flags;
}
struct StructByteIso {
private byte isovalue;
private byte flags;
}
struct StructByte1 {
private byte b1;
}
struct StructByte2 {
private byte b1, b2;
}
struct StructByte3 {
private byte b1, b2, b3;
}
struct StructByte4 {
private byte b1, b2, b3, b4;
}
struct StructByte5 {
private byte b1, b2, b3, b4, b5;
}
struct StructInt1 {
private int i1;
}
struct StructInt2 {
private int i1, i2;
}
struct StructInt3 {
private int i1, i2, i3;
}
struct StructInt4 {
private int i1, i2, i3, i4;
}
struct StructInt5 {
private int i1, i2, i3, i4, i5;
}
struct StructInt6 {
private int i1, i2, i3, i4, i5, i6;
}
struct StructFloat1 {
private float f1;
}
struct StructFloat2 {
private float f1, f2;
}
struct StructFloat3 {
private float f1, f2, f3;
}
struct StructFloat4 {
private float f1, f2, f3, f4;
}
struct StructFloat5 {
private float f1, f2, f3, f4, f5;
}
struct StructFloat6 {
private float f1, f2, f3, f4, f5, f6;
}
class ClassByte1 {
private byte b1;
}
class ClassFloat1 {
private float b1;
}
class ClassFloat2 {
private float b1, b2;
}
class ClassFloat3 {
private float b1, b2, b3;
}
class ClassFloat4 {
private float b1, b2, b3, b4;
}
class ClassFloat5 {
private float b1, b2, b3, b4, b5;
}
#pragma warning restore 0169
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment