Skip to content

Instantly share code, notes, and snippets.

@rms80
Created December 9, 2016 16:20
Show Gist options
  • Save rms80/771761c98dc96f17383cff63f754e2b2 to your computer and use it in GitHub Desktop.
Save rms80/771761c98dc96f17383cff63f754e2b2 to your computer and use it in GitHub Desktop.
profiling different ways to represent vector3f in C#
class vector1
{
public float x, y, z;
public vector1(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
public float this[int key]
{
get { return (key == 0) ? x : (key == 1) ? y : z; }
set { if (key == 0) x = value; else if (key == 1) y = value; else z = value; }
}
}
class vector2
{
public float x, y, z;
public vector2(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
public float this[int key]
{
get { if (key == 0) return x; else if (key == 1) return y; else return z; }
set { if (key == 0) x = value; else if (key == 1) y = value; else z = value; }
}
}
class vector3
{
public float[] v;
public vector3(float x, float y, float z) { v = new float[3]; v[0] = x; v[1] = y; v[2] = z; }
public float x { get { return v[0]; } set { v[0] = value; } }
public float y { get { return v[1]; } set { v[1] = value; } }
public float z { get { return v[2]; } set { v[2] = value; } }
public float this[int key]
{
get { return v[key]; }
set { v[key] = value; }
}
}
void startup_checks()
{
int R = 1000;
List<float> r = new List<float>();
for (int k = 0; k < R; ++k)
r.Add(UnityEngine.Random.Range(0.1f, 0.2f));
int N = 1000000;
float t0, t1;
int i;
double dSum;
for (int j = 0; j < 5; ++j) {
t0 = FPlatform.RealTime(); i = 0; dSum = 0;
var a = new vector1(1, 2, 3);
for (int k = 0; k < N; ++k) {
a.x += r[i++ % R];
a.y += r[i++ % R];
a.z += r[i++ % R];
dSum += a.x + a.y + a.z;
}
t1 = FPlatform.RealTime();
float TA = t1 - t0;
t0 = FPlatform.RealTime(); i = 0; dSum = 0;
var b = new vector1(1, 2, 3);
for (int k = 0; k < N; ++k) {
b[0] += r[i++ % R];
b[1] += r[i++ % R];
b[2] += r[i++ % R];
dSum += b[0] + b[1] + b[2];
}
t1 = FPlatform.RealTime();
float TB = t1 - t0;
t0 = FPlatform.RealTime(); i = 0; dSum = 0;
var c = new vector2(1, 2, 3);
for (int k = 0; k < N; ++k) {
c[0] += r[i++ % R];
c[1] += r[i++ % R];
c[2] += r[i++ % R];
dSum += c[0] + c[1] + c[2];
}
t1 = FPlatform.RealTime();
float TC = t1 - t0;
t0 = FPlatform.RealTime(); i = 0; dSum = 0;
var d = new vector3(1, 2, 3);
for (int k = 0; k < N; ++k) {
d[0] += r[i++ % R];
d[1] += r[i++ % R];
d[2] += r[i++ % R];
dSum += d[0] + d[1] + d[2];
}
t1 = FPlatform.RealTime();
float TD = t1 - t0;
t0 = FPlatform.RealTime(); i = 0; dSum = 0;
var e = new vector3(1, 2, 3);
for (int k = 0; k < N; ++k) {
e.x += r[i++ % R];
e.y += r[i++ % R];
e.z += r[i++ % R];
dSum += e.x + e.y + e.z;
}
t1 = FPlatform.RealTime();
float TE = t1 - t0;
DebugUtil.Log(2, "TIMES: xyz/xyz {0} xyz/idx? {1} xyz/idx-if {2} array/idx {3} array/xyz {4}",
TA, TB, TC, TD, TE);
}
results:
TIMES: xyz/xyz 0.01755667 xyz/idx? 0.04072809 xyz/idx-if 0.04195261 array/idx 0.03998852 array/xyz 0.0313077
TIMES: xyz/xyz 0.01719141 xyz/idx? 0.04062843 xyz/idx-if 0.04031706 array/idx 0.04036713 array/xyz 0.03364658
TIMES: xyz/xyz 0.01901817 xyz/idx? 0.0390439 xyz/idx-if 0.04326868 array/idx 0.04801798 array/xyz 0.04213095
TIMES: xyz/xyz 0.01736259 xyz/idx? 0.03987074 xyz/idx-if 0.03921556 array/idx 0.03991938 array/xyz 0.0328207
TIMES: xyz/xyz 0.01826048 xyz/idx? 0.05546379 xyz/idx-if 0.03929186 array/idx 0.03910398 array/xyz 0.03533506
my guess: branches in vector1/vector2 are optimized away when index is a constant, so same cost as array,
but there is some fixed overhead for using accessor vs public member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment